Error variable unexpected when executing my cmd file - windows

The issue is that when I put this line inside cmd file it doesn't work and I have an error on command line window
for /F "tokens=*" %i in (dir /S /B *.log) do del %i
Error :
Unexpected variable i (translated from french because I have french windows)
Any idea please ?

for /F "tokens=*" %%i in ('dir /S /B *.log') do del "%%i"
^^ ^ ^ ^^^ ^
Percent signs in for replaceable parameters should be escaped inside batch files, replacing the single % with %%.
To execute a command with a for /f, you need to enclose the command with single quotes or, if the command itself contains single quotes, with backquotes and include the usebackq clause in the for /f options.
It is better to quote the file references to avoid problems with spaces or special characters
Anyway, this command can be written as
del /s *.log
instead of executing a del operation for each file.

From command line:
for /F "tokens=*" %i in ('dir /S /B *.log') do del "%~i"
or if used in a batch:
for /F "tokens=*" %%i in ('dir /S /B *.log') do del "%%~i"
Explanation:
surround dir /S /B *.log with single quotes;
enclose file name in double quotes as follows del "%~i" as it could contain spaces...
Resources (required reading):
An A-Z Index of the Windows CMD command line
Windows CMD Shell Command Line Syntax

Thank you all of you for your helps. Please excuse my late answer.
Here is the solution I've finally adopted thanks to #MC ND
del /s *.log
del /s *.log0*
del /s *.tlog
rmdir /S /Q wls-admin\.wlnotdelete
rmdir /S /Q wls-myapp\.wlnotdelete

Related

For loop not finding windows file

I have a loop to open a file and find every file within but it wont find my file to open?
When i try to run i receive: - filenames.txt File not found - 0 files copied.
Code:
for /f "delims=" %%i in ("C:\user\userdata\filenames.txt") do echo D|xcopy "C:\user\userdata\files\%%i" "C:\Output\" /i /z /y
pause
The quotes between the parantheses tells for to process the filename a s string, not as pointer to a file. Either remove the quotes, or use usebackq (makes sense to keep them, as a filename may contain spaces):
for /f "usebackq delims=" %%i in ("C:\user\userdata\filenames.txt") do echo D|xcopy "C:\user\userdata\files\%%i" "C:\Output\" /i /z /y

Not able to get first 4 characters of the file name using forfiles in batch command

I'm trying to look for a particular file starting with C2319* from H:\temp folder and delete rest of the files greater than 7 days old
forfiles /P "H:\Temp\" /M *d6a0f2b4728a /D -7 /C "md /C for %I in (#path) do #if [%~nI]:~0,4==[c2319] del #file /F /A:S"
when executing the above command I'm getting error
4==[c2319] was unexpected at this time.
Could someone please let me know if I'm missing something.
your command line had a typo: md should be cmd
backslash before quote in "H:\Temp\" escapes the quote and should be removed
string extraction :~0,4 (should be 5, actually) only works for normal variables as it requires both opening and closing % or !, but since you'll have to assign the loop variable to a variable with delayed expansion (which works only inside batch files) it won't work in forfiles /c command.
Here's a faster solution using robocopy in list-mode:
#echo off
setlocal enableDelayedExpansion
set folder=H:\Temp
:: recursively list all *d6a0f2b4728a older than 7 days
for /f "delims=" %%a in ('robocopy /s /njh /njs /ns /nc /ndl /is /l /fp /minage:7 "%folder%" "%folder%" *d6a0f2b4728a') do (
:: trim spaces
set file=%%a&set file=!file:%folder%=*!
for /f "delims=* tokens=2" %%b in ("!file!") do (set file=%folder%%%b&set filename=%%~nb)
:: delete if not c2319*
if /i not "!filename:~0,5!"=="c2319" del /F /A:S "!file!"
)
pause

How to deal with an em dash in a filename

On a PC with Windows 7, I'm using a simple batch script to rename some Excel files, pre-pending their parent folder name:
for /f "delims=" %%i in ('dir /b /AD') do (
cd "%%i"
for /f "delims=" %%j in ('dir /b *.xl*') do ren "%%~j" "%%i_%%~j"
cd..
)
I noticed that some files generated an error, "System cannot find the specified file." These files all had an em dash (—) in the filename. After I changed the em dash to a regular hyphen (-), using Windows Explorer, the script worked fine on these files.
How can I help script the rename of these files?
Your problem is not with the batch variables: from the command line this works fine:
for %i in (*) do ren "%~i" "test_%~i"
but, as can be seen from:
for /f "delims=" %i in ('dir /b /a-d') do #echo ren "%~i" "test2_%~i"
dir /b is changing the em dash to a hyphen, and so the ren command clearly won't find the file to change.
For your examples you should find:
for /d %%i in (*) do (
and
for %%j in (*.xl*) do ...
should work fine.
If you need the dir /b for other reasons, I don't see a solution right now.
(I had a convoluted attempt exchanging all hyphens for question marks, using the "Environment variable substitution" and "delayed environment variable expansion" as described in SET /? and CMD /?, allowing any character to match, and then again use ren pattern matching to ignore the problem.
I.e.
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%I in ('dir /b /a-d') do (
Set K=%%I
ren "!K:-=?!" "test2_!K:-=?!"
)
but ren * x* replaces the start of the files with x, so the above replaces the hyphens with the content at that location before test_ was inserted.
So the best this approach can do is convert the em dashes to hyphens with:
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%I in ('dir /b /a-d') do (
Set K=%%I
ren "!K:-=?!" "test2_!K!"
)
)
And confirming it is the output of dir /b that is the problem: on the command line:
dir /b *—* > test.txt
where that — is an em dash, will only list the files with em dashes, but, in the output file, e.g. Notepad test.txt, you'll only find hyphens, and no em dashes.
BTW I've done all this testing on Windows 8.1 which VER displays as Microsoft Windows [Version 6.3.9600].
(As I mention above I did have ren * x* in this answer, but that replaces the first character with x rather than insert it, which I always thought ren did!)

batch file for loop with spaces in dir name

How do I modify this:
for /f %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"
to work when the path contains spaces?
For example, if this is run from
c:\my folder with spaces
it will echo:
c:\my
Thanks
You need to use:
for /f "delims=" %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"
This overrides the default delimiters which are TAB and SPACE
I got around this by prepending "type" and putting double quotes surrounding the path in the IN clause
FOR /F %%A IN ('type "c:\A Path With Spaces\A File.txt"') DO (
ECHO %%A
)
This article gave me the idea to use "type" in the IN clause.
If you don't want to deal with "quotes" you can use the "s" switch in %~dpnx[]...
this will output the short filenames that are easy to work with.
from the Command line...
for /f "delims=" %f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %~sdpnxf
inside a .CMD/.BAT file you need to "escape" the [%] e.g., double-up [%%]
for /f "delims=" %%f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %%~sdpnxf
The problem is there's a wildcard in the string that gets interpreted as a filename. You also need double quotes for a string with spaces. I'm not sure if there's a way to escape the wildcard.
for %a IN ("dir /b /s build\release\.dll") do echo %a
"dir /b /s build\release\.dll"

How to do something to each file in a directory with a batch script

How do you iterate over each file in a directory with a .bat or .cmd file?
For simplicity please provide an answer that just echoes the filename or file path.
Command line usage:
for /f %f in ('dir /b c:\') do echo %f
Batch file usage:
for /f %%f in ('dir /b c:\') do echo %%f
Update: if the directory contains files with space in the names, you need to change the delimiter the for /f command is using. for example, you can use the pipe char.
for /f "delims=|" %%f in ('dir /b c:\') do echo %%f
Update 2: (quick one year and a half after the original answer :-)) If the directory name itself has a space in the name, you can use the usebackq option on the for:
for /f "usebackq delims=|" %%f in (`dir /b "c:\program files"`) do echo %%f
And if you need to use output redirection or command piping, use the escape char (^):
for /f "usebackq delims=|" %%f in (`dir /b "c:\program files" ^| findstr /i microsoft`) do echo %%f
Alternatively, use:
forfiles /s /m *.png /c "cmd /c echo #path"
The forfiles command is available in Windows Vista and up.
Easiest method:
From Command Line, use:
for %f in (*.*) do echo %f
From a Batch File (double up the % percent signs):
for %%f in (*.*) do echo %%f
From a Batch File with folder specified as 1st parameter:
for %%f in (%1\*.*) do echo %%f
Use
for /r path %%var in (*.*) do some_command %%var
with:
path being the starting path.
%%var being some identifier.
*.* being a filemask OR the contents of a variable.
some_command being the command to execute with the path and var concatenated as parameters.
Another way:
for %f in (*.mp4) do call ffmpeg -i "%~f" -vcodec copy -acodec copy "%~nf.avi"
I had some malware that marked all files in a directory as hidden/system/readonly. If anyone else finds themselves in this situation, cd into the directory and run for /f "delims=|" %f in ('forfiles') do attrib -s -h -r %f.

Resources