Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Batch Script that has error.

Posted on 2014-04-28
5
Medium Priority
?
431 Views
Last Modified: 2014-05-23
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"
0
Comment
Question by:ben1211
5 Comments
 
LVL 24

Expert Comment

by:DMTechGrooup
ID: 40028648
Permission issue?  What happens if you right click and run as administrator?
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 2000 total points
ID: 40028653
I think you need to add one more % for %m like this:

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" 

Open in new window

0
 

Author Comment

by:ben1211
ID: 40028660
I get the same error, when I run it as Administrator. I'll try what pkwan says.
0
 
LVL 3

Expert Comment

by:telczj9
ID: 40028697
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!
0
 
LVL 85

Expert Comment

by:oBdA
ID: 40028857
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

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question