How to tell forfiles to execute command in PATH? - batch-file

I'm missing something (obvious?) about escaping my strings or spaces in the following Windows Server 2k3 batch command.
FORFILES -m *.wsp -c "CMD /C C:\Program^ Files\Common^ Files\Microsoft^ Shared\web^ server^ extensions\12\bin\stsadm.exe^ -o^ addsolution^ -filename^ #FILE"
Results in the following error
'C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\bin\stsadm.exe -o addsolution -filename "foobar.wsp"' is not recognized as an internal or external command,operable program or batch file.
But I can't figure out why. I'm working off Mr. Simon Sheppard's fine documentation

The path needs to be quoted, and the quote must be escaped.
FORFILES -m *.wsp -c "CMD /C ^0x22C:\Program^ Files\Common^ Files\Microsoft^ Shared\web^ server^ extensions\12\bin\stsadm.exe^0x22 -o^ addsolution^ -filename^ #FILE"
A co-worker suggested using the hex for ", and I eventually figured out that the hex needed escaping.

Another possible answer is to use the old 8.3 names you get by doing dir /X.
Like: C:\PROGRA~1 instead of C:\Program Files.

Related

forfile and AD homefolders doesnt work

we have an active directory with about 80 users.
what i want to do now is place in each userfolder a script that the user can run for themselves and it makes a log file in that same folder with files older than 5 days.
The homefolder for each user is mapped as drive in windows to the letter h
right now i have this:
forfiles -p "%cd%" -s -m *.* /D -5 /C "cmd /c echo #path >> %cd%\log.txt"
but it throws me this error.
H:\>forfiles -p "H:\" -s -m *.* /D -5 /C "cmd /c echo #path >> H:\\log.txt"
ERROR: Invalid argument/option - '#path'.
is there any way to solve this?
if i run in on my local pc with a test folder it works fine.
When it comes to the closing doublequote, if you move it to before your redirection it certainly would make more sense, as would changing it to > instead of >>.
The fix to your issue, is when you use the /P option, you cannot doublequote it, and also include a trailing backslash, (it's one or the other).
My reading is that the backslash is escaping the doublequote.
Because drives tend to require a trailing backslash, all you have to do is remove the doublequotes around it, (they're not needed anyhow because drives don't include spaces).
Also please remember that, as already inferred in my comment, ForFiles already uses the current directory by default so removing it entirely is easier.

SQLCMD command, How to save output into log file

The following question has helped me solving the problem of executing multiple SQL Scripts located in file. Run all SQL files in a directory
However, I did not get how to redirect the output into a separate log file. Someone suggested the following script but since I don't understand it, it did not work and I can't find out the error.
for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" >> sql.log 2>&1)
If you need the output into one common file then you should use the #Abhishek 's answer.
If you need the output into a separate log file for an each input sql file
then you can use -o parameter of sqlcmd command. Your bat file could look like this:
for %%G in (*.sql) do sqlcmd /S <servername> /d <dbname> -E -i"%%G" -o C:\logs\%%G.log
pause
In this case for
1.sql
2.sql
you will get:
1.sql.log
2.sql.log
You are seeking Command Redirection.
As per your example -
for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f" >> sql.log 2>&1
once the execution of the sql script is done the output will be redirected to and appends the command output to the end of file (here sql.log) without deleting the information that is already in the file (>>) and redirects STDERR (2) into STDOUT handle(1) - 2>&1
More information here and here.

If Batch File Modified Date is older than 90 days, delete files

Basically what I'm trying to do is create a batch file and place it in my startup that will use the modified date of the same batch file and see if it is greater or less than 90 days old. If it isn't, nothing happens and life goes on, but if it is, I want it to delete the contents of my downloads folder. I have a little bit of coding, but I've been testing it, but for some reason it isn't wanting to work for me. Any help would be greatly appreciated.
forfiles -p "C:\LOCATION OF .BAT\" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
I'm just starting out using batch commands and would really love the help
This works for you if you are using Win2003 or WinXP and have forfiles.exe installed on your machine...
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat -d -90 -c "cmd /c del C:\user folder\Downloads"
Later versions of Windows and Windows Server have it installed by default.
For Win7 or higher: Syntax has changed a little therefore the updated command is:
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
Assuming your target path contains spaces etc, then use this in your forfiles command. The 0x22 represents a double quote character.
If it echos the command then try it without the echo to actually perform the deletion
"cmd /c echo del 0x22C:\user folder\Downloads\*.*?0x22"

using sqlcmd in batch file

I've scoured StackOverflow for a few hours, and tried different suggestions for similarly asked questions, but nothing passed the parameters correctly so far (double quotes, ^).
Here's the short version of it:
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
start /w "sqlcmd" sqlcmd.exe -S DBserverName -U username -P p#ssword -i C:\query.sql -s"," | findstr /V /C:"-" /B >c:\output.csv
Basically, I want to pass a rather long parameter containing delimiters. But the only way I see that happen is to use arguments. I'd like to keep it as simple as possible. Is this the only recourse? Can you offer an example how this might work?
Thanks in advance.
I'm not sure if it matters, but I think there should be a space between -s and ","
But more importantly, your pipe construct is wrong. Your sqlcmd command is running in a new window, but your pipe is looking for output from the START command itself in the original window - and there isn't any.
You could get your command to work by escaping the pipe and redirection.
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
start /w "sqlcmd" sqlcmd.exe -S DBserverName -U username -P p#ssword -i "C:\query.sql" -s "," ^| findstr /V /C:"-" /B ^>"c:\output.csv"
But there is no need to use START at all. Your script can simply execute sqlcmd directly, and everything is much simpler.
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
sqlcmd.exe -S DBserverName -U username -P p#ssword -i "C:\query.sql" -s "," | findstr /V /C:"-" /B >"c:\output.csv"
You might also be running into problems with your password, depending on what characters are used. You might have to quote and/or escape the password, and sqlcmd.exe might have its own escape rules. If it does, then you might have to worry about escaping for both cmd.exe and sqlcmd.exe.
This line should have double quotes around the entire path, in some cases it matters.
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
and the start command needs double quotes at the beginning because it takes the first set as the window title.
start "" /w ....

Forfiles is not working in batch file but it is working in command line

FORFILES -pc:\tempfolder -s -d-6 -m* -c "CMD /C if #ISDIR==TRUE RD /S /Q #FILE"
is not working in batch file but it is working in command line
I am using older version of forfiles for windows xp. As mentioned above forfiles is working in command prompt, but when i copy the same command to batch file it's giving can't execute (error 2).
http://web.archive.org/web/20150527024532/http://www.sharedcache.com/cms/tips_and_tricks.aspx
Click Download
Drop Forfiles.exe C:\
C:\forfiles.exe -p "C:\Documents and Settings\test\My Documents\Downloads\Test file" -s -m . /C "cmd /c del #path" /d -14
Copy a old computer .exe file in the above location (don't drag it or you lose it forever.)
Change the "-14" to any days with a newer file in the same location, (make sure you copy it) run the batch file.
Now change "-14" to "-1", both files should be gone if you did it correctly.
Now set up a schedule to run it every whatever days for your heart contempts.
wrote this here bc I keep running in here from google and so many sites doesn't say you needed fortfiles.exe program itself for the cmd prompt from seeing any errors.
You can test the file your self and list the files wherever forfile.exe is located in. It is safe to run bc it doesn't have a cmd batch file to delete.
Enjoy!
ps. you can Drop the "forfiles" here "C:\WINDOWS\system32\"
without having to add "C:\forfiles.exe" in the cmd prompt and run it.

Resources