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
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 need a batch file that counts certain file types in sub directories. In this case it's .txt and .xls files. At the end of the file it reports this back.
#echo off
REM get script directory
set scriptdir=%~dp0
REM change directory to script directory
cd /d %scriptdir%
setlocal
set txtcount=0
set xlscount=0
for %%x in ('dir *.txt /s ) do set /a txtcount+=1
for %%x in ('dir *.xls /s ) do set /a xlscount+=1
echo %txtcount% text files
echo %xlscount% .xls files
endlocal
pause
My batch file doesn't report back the correct count of files. I thought it might be continually counting but I've set the count variables to local so I'm not sure what's up.
There's three problems with your script:
You want to iterate over command output, so you need FOR /F
You're not using the "bare" /B format, i.e. DIR outputs more than just filenames
You're missing the closing single quote twice
Replace your loops with this:
FOR /F %%X IN ('DIR /S /B *.txt') DO SET /A "txtcount+=1"
FOR /F %%X IN ('DIR /S /B *.xls') DO SET /A "xlscount+=1"
You are very close, but you are missing the switch /B of the dir command, so the output contains a header and a footer, which are both included in the count. Therefore, change dir *.txt /s to dir *.txt /s /b (same for *.xls) and the count is going to be correct.
Besides that, there are typos: the closing ' is missing -- for %%x in ('dir *.txt /s') do .... In addition, the /F switch at for is missing, which is needed to capture the output of a command. (I assume these things are present in the true script as you would receive errors otherwise.)
Alternative Approach
To count the number of files, you could also do the following:
for /F "delims=" %%X in ('dir /B /S "*.txt" ^| find /C /V ""') do set "COUNT=%%X"
find /V "" searches for non-empty lines, hence all lines returned by dir /B /S are considered as matches; the /C switch simply lets find return the total amount of matching lines.
I'm using Windows and I have a huge list of files
something nextthing.ext
other something.ext
banana apple.ext
I'm trying to generate the following:
something nextthing.rar
other something.rar
banana apple.rar
I've done some research and I couldn't find how to do this. I know WinRAR has a built in way to do this, but it won't let me keep the names. It just renames them to Foldername1.rar, Foldername2.rar ....
I tried using the following command line:
for /f "delims=|" %f in ('dir /b "C:\Users\Adam\
Desktop\Files"') do rar a %f *.rar
However, it doesn't generate all files and they seem to be named wrongly...
For example, what should be banana apple.rar becomes banana.rar and apple.rar
Okay, found how....
#echo off &setlocal
set "path=C:\Program Files\WinRAR;%path%"
:indiv
echo(
echo(
FOR %%i IN (*) do (
rar a "%%~ni.rar" "%%~i" || echo Error building archive!
)
You can use in a command prompt window to compress each file in a directory into a separate archive:
for /f "delims=" %f in ('dir "%USERPROFILE%\Desktop\Files" /B /A-D') do "%ProgramFiles%\WinRAR\Rar.exe" a -ep1 -idcdp "%~nf.rar" "%f"
Important are the double quotes around %f as some file names contain 1 or more spaces and in this case the file names must be enclosed in double quotes.
Above command for usage in a batch file:
#echo off
for /f "delims=" %%f in ('dir "%USERPROFILE%\Desktop\Files" /B /A-D') do "%ProgramFiles%\WinRAR\Rar.exe" a -ep1 -idcdp "%%~nf.rar" "%%f"
This command ignores subfolders because of /A-D. It compresses only each file in directory Files on desktop of current user.
Hello i want to create a batch file that copies the files from the current directory to another.
for /F "usebackq" %%b IN (`DIR /B /S ""`) DO #(
XCOPY %%b %1
)
So Far so good.
MY problem are the whitespaces in directories.
So when the name of a directory is /Dir whitespaces end/
it does not copy it. "File not found - Dir"
Start bat file with destination
CopyFiles.bat "I:\testFolder*.*"
How can i work around this feature?
Try this:
for /f "delims=" %%b in ('dir /b /s ') do xcopy "%%~b" "%1"
Important is 1) set "delims=" and 2) enclose for loop and other variables in double quotes.
In a Windows cmd batch script named my.bat, I want to execute a command for all the files in the current directory except for the batch file my.bat.
I use below command in my.bat currently to run the command only for *.txt *.ppt, but really as new files of different extensions might be added to the folder and hence execution of this command by excluding one type of file extension (in my case *.bat) would be more readable/helpful.
FOR /F "delims=|" %%i IN ('dir /b *.txt *.ppt') DO echo %%i
Question is: How do I exclude that file alone with that particular file extension and make the for command execute on all files of all extensions except the excluded one?
FOR /F "tokens=* delims=|" %%i IN ('dir /b *.*') do if not %%~xi==.bat echo %%i
I have added tokens=* in as well otherwise you won't get full filenames if they have spaces.
To echo without the dot
setlocal enabledelayedexpansion
FOR /F "tokens=* delims=|" %%i IN ('dir /b *.*') do (
set e=%%~xi
set e=!e:.=!
echo !e!
)
This is providing that the file doesn't have any other dots, otherwise it will remove them too. This is a bit more sturdy than just removing the 4th character from the end though, as not all files have a 3 character extension.
You could pass the output of the dir /b command through findstr, like so:
FOR /F "delims=|" %%i IN ('dir /b ^| findstr /v /r "^my.bat$"') DO echo %%i
The /v option to findstr prints anything that doesn't match the parameter. The match is based on a regular expression that matches only lines that contain my.bat and nothing else.