DOS batch file variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Janning
Posts: 5
Joined: 20 Jun 2015 17:18

DOS batch file variable

#1 Post by Janning » 20 Jun 2015 17:22

Hi, I have an easy question. I'm using below in a batch file:

forfiles.exe /p "B:\iCamSource_videos" /s /m *.jpg /d -11 /c "cmd /c del @file"

The "-11" tells it to erase files older than 11 days.

since this is one of many command lines I have, i'd like one variable to replace the -11...something like:

SET l_days = -1

forfiles.exe /p "B:\iCamSource_videos" /s /m *.jpg /d "%l_days%" /c "cmd /c del @file"

But, this is not the correct syntax...please advise, and thanks!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DOS batch file variable

#2 Post by foxidrive » 20 Jun 2015 17:53

Remove the double quotes that you added and try it.

Janning
Posts: 5
Joined: 20 Jun 2015 17:18

Re: DOS batch file variable

#3 Post by Janning » 20 Jun 2015 18:11

Trying below:

SET l_days = "-1"

forfiles.exe /p "B:\iCamSource_videos" /s /m *.jpg /d %l_days% /c "cmd /c del @file"

Gets error: (I should have noted I had already tried this before my post)

E:\APPS\BATCH_JOBS>forfiles.exe /p "B:\iCamSource_videos" /s /m *.jpg /d /c "cm
d /c del @file"
ERROR: Invalid syntax. Value expected for '/d'.
Type "FORFILES /?" for usage.

Janning
Posts: 5
Joined: 20 Jun 2015 17:18

Re: DOS batch file variable

#4 Post by Janning » 20 Jun 2015 18:15

Hi,
I have already tried it without the quotes before making this post:

SET l_days = "-1"
forfiles.exe /p "B:\iCamSource_videos" /s /m *.jpg /d %l_days% /c "cmd /c del @file"

ERROR: Invalid syntax. Value expected for '/d'.
Type "FORFILES /?" for usage.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DOS batch file variable

#5 Post by foxidrive » 20 Jun 2015 18:57

You put quotes back in it, in a different place.

Janning
Posts: 5
Joined: 20 Jun 2015 17:18

Re: DOS batch file variable

#6 Post by Janning » 20 Jun 2015 21:14

? are you saying that I "did"",,, or that I should? either way, I've tried every variations of "% and @...not just what I am posting on here. I get same error.
can u give me an example of what it should be?
thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: DOS batch file variable

#7 Post by foxidrive » 20 Jun 2015 21:48

I wasn't paying close enough attention myself - I failed to see where you had a space in the environment variable name.

Try this:

Code: Select all

 SET l_days=-1

 forfiles.exe /p "B:\iCamSource_videos" /s /m *.jpg /d %l_days% /c "cmd /c del @file"


This term SET l_days = -1 is an environment variable called %SET l_days % with that extra trailing space, and it sets the contents of the environment variable to " -1" sans the quotes. That extra leading space is in the variable data itself too.

I saw the extra space that was before the minus sign but I didn't comment because it didn't affect the position it was being used in.

I hope that works for you...

Time for a snooze...

Janning
Posts: 5
Joined: 20 Jun 2015 17:18

Re: DOS batch file variable

#8 Post by Janning » 21 Jun 2015 02:05

Thanks, it all works very well...much appreciated!

Post Reply