Use one button to go to a folder, and also delete all *.log?

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
21thermostat
Junior Member
Junior Member
Posts: 2
Joined: 2015-08-01, 17:05 UTC

Use one button to go to a folder, and also delete all *.log?

Post by *21thermostat »

Is it possible to use one button to go to a folder, and also delete all *.log file?


I have a button in the tool bar, the command is "cd c:\temp". When I click the button, the totalcmd would go to the directory c:\temp.

But I also want to delete all *.log file when I click the button. Is it possible?



Thank.

Jim
User avatar
karlchen
Power Member
Power Member
Posts: 4605
Joined: 2003-02-06, 22:23 UTC
Location: Germany

Post by *karlchen »

Hello, Jim.

I guess the following commandline should work

Code: Select all

cmd.exe /c "cd /d C:\temp & del *.log"
Cheers,
Karl
21thermostat
Junior Member
Junior Member
Posts: 2
Joined: 2015-08-01, 17:05 UTC

Post by *21thermostat »

Thanks. It works.Perfect.
User avatar
MVV
Power Member
Power Member
Posts: 8711
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

But it would be safer to do

Code: Select all

cmd.exe /c "del C:\temp\*.log"
instead, just for case when cd fails and del then deletes files from another directory.
User avatar
milo1012
Power Member
Power Member
Posts: 1158
Joined: 2012-02-02, 19:23 UTC

Post by *milo1012 »

MVV wrote:But it would be safer to do

Code: Select all

cmd.exe /c "del C:\temp\*.log"
instead, just for case when cd fails and del then deletes files from another directory.
You heard of errorlevel?

Code: Select all

cmd.exe /c "cd /d C:\temp & IF NOT ERRORLEVEL 1 (del *.log)"
Last edited by milo1012 on 2015-08-03, 02:05 UTC, edited 1 time in total.
TC plugins: PCREsearch and RegXtract
User avatar
MVV
Power Member
Power Member
Posts: 8711
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

milo1012,
Why are you doing 2 operations and check for error if one does completely the same thing?

BTW your command may be written in a shorter way:

Code: Select all

cd /d C:\temp && del *.log
Here && executes second command only if first one succeeded. Also || exists that does the opposite.

Anyway, my command is simpler and does what was asked.
User avatar
milo1012
Power Member
Power Member
Posts: 1158
Joined: 2012-02-02, 19:23 UTC

Post by *milo1012 »

MVV wrote:Why are you doing 2 operations and check for error if one does completely the same thing?
...
Here && executes second command only if first one succeeded
Because that very syntax may be also used in a standalone batch file, and not just squeezed as a parameter to cmd.exe,
in case you want to swap the CD command with something else and need a check for errorlevel then.
TC plugins: PCREsearch and RegXtract
User avatar
MVV
Power Member
Power Member
Posts: 8711
Joined: 2008-08-03, 12:51 UTC
Location: Russian Federation

Post by *MVV »

You can use && and || in batch file too. cmd.exe /c processes simply a single batch line, with same rules applied.
Anyway, it is a bad style to mix multiple commands in a line, especially such as cd which affects next lines too.
User avatar
milo1012
Power Member
Power Member
Posts: 1158
Joined: 2012-02-02, 19:23 UTC

Post by *milo1012 »

MVV wrote:You can use && and || in batch file too. cmd.exe /c processes simply a single batch line, with same rules applied.
And that is related to my previous answer, because...?
I just said that you can simply copy and paste common commands schemes that way, so that you may not have to have to think through every altered command.
BTW, with the errorcheck you can keep the batch open and output some message in case of errors.
MVV wrote:Anyway, it is a bad style to mix multiple commands in a line
By whose standard? There is no correct or wrong when it comes to multiple solutions for problems like the OP.
MVV wrote:...especially such as cd which affects next lines too.
And other built-in things like mkdir and copy don't effect the next lines?
Most things in a batch may be effected by previous lines. We really shouldn't discuss platitudes here.
All solutions work.
TC plugins: PCREsearch and RegXtract
Post Reply