Solved

Batch Script that has error.

Posted on 2014-04-28
5
424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

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…
Preface There are many applications where some computing systems need have their system clocks running synchronized within a small margin and eventually need to be in sync with the global time. There are different solutions for this, i.e. the W3…
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…
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

636 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