Link to home
Start Free TrialLog in
Avatar of ben1211
ben1211Flag for Malaysia

asked on

Batch Script that has error.

I have this particular script. When I run it from a batch (.bat) file, I receive an error seen below. But when I open a command prompt window and I run it, I don't receive any errors. Could you help me out please?


ERROR RECEIVED:
C:\>test2.bat
\users`) was unexpected at this time.

C:\>for /f "usebackq delims=" \users`) do forfiles -p "C:\USERS\m\DESKTOP" -s -m
 *.txt /D -7 /C "cmd /c del @path"

     


THE SCRIPT:


for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\DESKTOP" -s -m *.txt /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\DESKTOP" -s -m *.xls /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\DESKTOP" -s -m *.xlsx /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\DESKTOP" -s -m *.xlsm /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\DESKTOP" -s -m *.pdf /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\DESKTOP" -s -m *.doc /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\DESKTOP" -s -m *.docx /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\DESKTOP" -s -m *.vis /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\DESKTOP" -s -m *.ice /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\Documents" -s -m *.txt /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\Documents" -s -m *.xls /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\Documents" -s -m *.xlsx /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\Documents" -s -m *.xlsm /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\Documents" -s -m *.pdf /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\Documents" -s -m *.doc /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\Documents" -s -m *.docx /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\Documents" -s -m *.vis /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\Documents" -s -m *.ice /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m" -s -m *.txt /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m" -s -m *.xls /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m" -s -m *.xlsx /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m" -s -m *.xlsm /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m" -s -m *.pdf /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m" -s -m *.doc /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m" -s -m *.docx /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m" -s -m *.vis /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m" -s -m *.ice /D -2 /C "cmd /c del @path"
for /f "usebackq delims=" %m in (`dir /b c:\users`) do forfiles -p "C:\USERS\%m\AppData\Local\Microsoft\Windows\WebCache" -s -m *.* /D -2 /C "cmd /c del @path"
Avatar of DMTechGrooup
DMTechGrooup
Flag of United States of America image

Permission issue?  What happens if you right click and run as administrator?
ASKER CERTIFIED SOLUTION
Avatar of Peter Kwan
Peter Kwan
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ben1211

ASKER

I get the same error, when I run it as Administrator. I'll try what pkwan says.
Avatar of telczj9
telczj9

If you run the for loop in a batch you need two percent symbols for the variable if you run it on the command line window then you should use only one percent symbol.


Cheers!
Avatar of oBdA
Apart from the missing % sign for the loop variable, you're using an editor that turns single quotes into ("pretty") back ticks; the "dir ..." expression in the "for /f" needs to be enclosed in ['] (a single quote), not [`] (back tick).
Anyway, here's a shorter version; it's in test mode and will only display the commands it would normally run. Remove the uppercase ECHOs in lines 10, 14, 16 to run it for real:
@echo off
setlocal
set FolderRoot=C:\Users
set FolderList="DESKTOP" "Documents"
set ExtensionList=.txt .xls .xlsx .xlsm .pdf .doc .docx .vis .ice
for /d %%a in ("%FolderRoot%\*.*") do (
	echo Processing user '%%~nxa' ...
	for %%d in (%FolderList%) do (
		for %%f in (%ExtensionList%) do (
			ECHO forfiles -p "%%a\%%~d" -s -m *%%f /D -2 /C "cmd.exe /c del @path"
		)
	)
	for %%f in (%ExtensionList%) do (
		ECHO forfiles -p "%%a" -s -m *%%f /D -2 /C "cmd.exe /c del @path"
	)
	ECHO forfiles -p "%%a\AppData\Local\Microsoft\Windows\WebCache" -s -m *.* /D -2 /C "cmd.exe /c del @path"
)

Open in new window