Solved

Batch Script that has error.

Posted on 2014-04-28
5
417 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 500 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 83

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RDM Dependencies on VMware 2 82
How to convert MFC::CString to UTF8 wchar_t* 10 216
SQL Query - Issue with Top Statement 5 46
"Black Box" Testing of Control System Software 2 53
INTRODUCTION The purpose of this document is to demonstrate the Installation and configuration of the Data Protection Manager product. Note that this demonstration was prepared on the basis of Windows OS is 2008 R2 and DPM 2010. DATA PROTECTI…
Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
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…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

831 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