FOR /F "tokens=*" %%g IN ('move a.txt b.txt') do (SET VAR=%%g)
How can I suppress error message of MOVE? Without FOR, I can do >nul. But in FOR, >nul is getting error.
Thanks!
I tried to put >nul before move, within single quote, or after single quote. None of them worked
Related
I'm working on a project in batch-file and need to read the contents of a file and print out the contents. I have a set up a for /f loop to go through the contents of the file, but it isn't working.
The Code
cd C:\Users\(name)
for /f %%G in (List.txt) (
del /F /S %%G
echo %errorlevel%
pause
)
I have been Googling for about an hour now, and can't find anything which has worked for me. I was hoping that you guys could tell me what I'm doing wrong with my code.
Default delimiters in cmd for the for /F loop is whitespace. Your code will split and assign the first word/number/line up until the first whitespace.
You need to tell the for /f loop to not use any delimiters. Also usebackq in order for you to double quote your file as that can also be a full path to the file with whitespace, i.e: "C:\My Documents\Old Files\list.txt"
#echo off
for /f "usebackq delims=" %%i in ("List.txt") do del /F /S "%%~i"
then, del does not set the errorlevel and you will always get 0 returned. if you really want to check the result, redirect stderr to stdout and use findstr to determine if delete was successful.
I wonder what's wrong with my coding, because it's not working
I want to rename all the png files inside Chris.
but it failed
for /f in ('C:/Users/Chris/Downloads/images/*.png')
do ren "C:\Users\Chris\Downloads\images\*.png" "%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%-img.png"
No need for /f in argument, no need for quotes but your missing a variable declaration
The variable should be used in the do-part otherwise the for is not realy helpful
the for will enumerate the full path so you need to strip the filename using ~n
the do-part must be directly behind the for-statement or it needs to be inside round brackets
here's the complete code:
for %%i in (C:/Users/Chris/Downloads/images/*.png) do (
ren "%%i" "%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%-%%~niimg.png"
)
If order to use a for loop, you need to specify a variable to use (even if you don't use a variable in the loop at all), otherwise you'll get a syntax error. While variables can only be one letter, this is pretty much the only time in batch that variables are case-sensitive, so you've got 52 letters, plus a few additional characters that I've seen used, like #. Additionally, do must always be on the same line as the ).
A for /F loop can process strings, text files, and other batch commands.
To process strings, use double quotes: for /F %%A in ("hello world") do echo %%A
To process batch commands, use single quotes: for /F %%A in ('dir /b') do echo %%A
To process text files, do not use any quotes at all: for /F %%A in (C:\Users\Chris\image_list.txt) do echo %%A
You may also want to go into the directory that you're processing just to make things easier.
pushd C:\Users\Chris\Downloads\images
for /F %%A in ('dir /b *.png') do (
REM I'm not sure what the %HR% variable is supposed to be, so I'm ignoring it.
ren "%%A" "%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%-img.png"
)
Searching, trying and crying I developed a code:
For /R %%G IN (*.txt) do for /f "delims=" %%a in ('more +1 %%G ^| find /v ""') do (set line=%%~a echo !line!) > new\%%G
Do someone know why does it loop forever?
What it is expected to do, is to remove blank lines in every *.txt file it founds in all subdirectories and put the new file with the same name in "new" folder. I can provide all "new" folders needed manually.
I'm not sure this would cause it to loop forever, but you are writing to a NEW folder that is in the same hierarchy that you are reading from. So it will process files that you just wrote. You will need a filter to prevent that.
Also, I think your output file name needs %%~nxG to get just the file name and extension, without the path.
There is a much simpler way to strip out blank lines. findstr . file will strip out empty lines. findstr /rc:"[^ ] will strip out empty lines and lines that contain only spaces.
#echo off
setlocal
set "outFolder=new"
for %%F in ("%outfolder%\.") for /r %%G in (*.txt) do if "%%~dpG" neq "%%~fF\" findstr /rc:"[^ ]" "%%G" >"%%~fF\%%~nxG"
But the simplest thing to do is just make sure your output folder is distinct from your source hierarchy
for /r %%G in (*.txt) do findstr /rc:"[^ ]" "%%G" >"\distinctPath\%%~nxG"
UPDATE
One thing that could cause your script to hang is that piped MORE will hang if the file has more than 64k lines. But I don't understand why you are using MORE in the first place.
I program a batch file, but I'm new to for-loops which I need.
I now know how the syntax works, but I cannot figure out why my loop does not do what it should.
This code is an extract from my file:
#echo off & setlocal enabledelayedexpansion
set /p file=:
set /a numberofgoals=0
for /f "delims=" %%a in ("%file%.txt") do set /a "numberofgoals+=1"
echo %numberofgoals%
pause > nul
If I did everything right my output should be the length of the specified textfile and it worked for me before, but apperently I changed something in the code that I'm not sure about and the output of %numberofgoals% is everytime exactly 1 now, regardless of how long my text file is.
My question is: What have I done wrong and why is the output 1 now? I cannot even remember having changed something there...
EDIT: I changed "delims=" into "usebackq delims=" as suggested and it works now, thank you.
The quotes in the for loop's parentheses mean "process this string" rather than "read this file". Use the usebackq option to indicate that the quotes are providing a filename:
for /f "usebackq delims=" %%a in ("%file%.txt") do set /a "numberofgoals+=1"
and you should be golden.
Type help for in your cmd window for the gory details.
I'm trying to copy multiple files and folders from a drag and drop selection using a solution I think should look something like this:
mkdir newdir
for %%a in ("%*") do (
echo %%a ^ >>new.set
)
for /f "tokens=* delims= " %%b in ('type "new.set"') do (
SET inset=%%b
call :folderchk
if "%diratr%"=="d" robocopy "%%b" "newdir" "*.*" "*.*" /B /E && exit /b
copy /Y %%b newdir
)
exit /b
:folderchk
for /f tokens=* delims= " %%c in ('dir /b %inset%') do (
set atr=%~ac
set diratr=%atr:~0,1%
)
I've tried throwing together code from the following examples but I'm stuck:
http://ss64.com/nt/syntax-dragdrop.html
Drag and drop batch file for multiple files?
Batch Processing of Multiple Files in Multiple Folders
Handling of special characters with Drag&Drop is tricky, as doesn't quote them in a reliable way.
Spaces are not very complicated, as filenames with spaces are automatically quoted.
But there are two special characters, who can produce problems, the exclamation mark and the ampersand.
Names with an ampersand will not automatically quoted, so the batch can be called this way
myBatch.bat Cat&Dog.txt
This produces two problems, first the parameters aren't complete.
In %1 and also in %* is only the text Cat the &Dog.txt part can't be accessed via the normal parameters, but via the cmdcmdline variable.
This should be expanded via delayed expansion, else exclamation marks and carets can be removed from the filenames.
And when the batch ends, it should use the exit command to close the cmd-window, else the &Dog.txt will be executed and produce normally an error.
So reading the filenamelist should look like
#echo off
setlocal ENABLEDELAYEDEXPANSION
rem Take the cmd-line, remove all until the first parameter
set "params=!cmdcmdline:~0,-1!"
set "params=!params:*" =!"
set count=0
rem Split the parameters on spaces but respect the quotes
for %%N IN (!params!) do (
echo %%N
)
pause
REM ** The exit is important, so the cmd.ex doesn't try to execute commands after ampersands
exit
This is also described in the link you refereneced Drag and drop batch file for multiple files?