Solved

Batch Script that has error.

Posted on 2014-04-28
5
413 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
Comment Utility
Permission issue?  What happens if you right click and run as administrator?
0
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 500 total points
Comment Utility
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
Comment Utility
I get the same error, when I run it as Administrator. I'll try what pkwan says.
0
 
LVL 3

Expert Comment

by:telczj9
Comment Utility
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 82

Expert Comment

by:oBdA
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Many admins will agree: WSUS is is a nice invention but using it on the client side when updating a newly installed computer is still time consuming as you have to do several reboots and furthermore, the procedure of installing updates, rebooting an…
The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now