I have the following batch statement:
for /f "delims=" %%x in (file.lst) do set "offendingfile=%%x"
Although for some really odd reason, when it is called it outputs:
"C:\Windows\calc.exe "
instead of
"C:\Windows\calc.exe"
Since there is a trailing space, I can't use it properly with any other statements in the batch file, does anyone know why it does this and how to fix this, as its been driving me nuts!
does your file.lst file has a trailing space after the file name?
I checked this with file.lst having: c:\windows\calc.exe and the output was correct, but if the file.lst file contains c:\windows\calc.exe<SPACE>, the output is the same that you are getting (and is the expected output as well).
I believe that the delims= portion of the for statement is removing the default behavior of using spaces as delimiters. If you remove that portion, then it should remove the trailing blank:
for /f %%x in (file.lst) do set "offendingfile=%%x"
Related
I've got about 700 .tcx files (old GPS running data if you're interested...).
The first line is:
<?xml version="1.0" encoding="UTF-8"?>
That's 10 spaces at the start, which is preventing me from importing this data to Garmin Connect (I got a new Garmin watch).
So, I need to remove this whitespace from the front of this first line of 700 files. I'm trying to automate this process with a batch file (though given how long I've spent trying to do this, it would've been quicker to do it by hand...)
So far I've got:
#echo off
setlocal enabledelayedexpansion
for /F "tokens=1" %%A in (C:\[path]\[filename].tcx) do (
set line=%%A
echo !line:~1! >> C:\[path]\[filename].tcx
)
endlocal
Where [path] is the path to where the files are currently stored, and filename is the file I'm testing on. Once I've got it working I will replace [filename[ with *
Unfortunately what I've got isn't quite working at the moment:
Firstly, it is going through the whole file, not just the first line.
Secondly, on each line, it is not just deleting the leading whitespace, it is deleting everything upto and including the first character (which is a "<") and then also deleting everything after the next space that is comes across.
I know my attempt is kind of pathetic, but I'm hoping to learn!
#ECHO OFF
SETLOCAL
rem The following settings for the directories are names
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
FOR %%e IN ("%sourcedir%\*.tcx") DO (
SET "first=y"
(
FOR /f "usebackqtokens=*" %%y IN ("%%e") DO IF DEFINED first SET "first="&ECHO %%y
FOR /f "usebackqskip=1delims=" %%y IN ("%%e") DO ECHO %%y
)>"%destdir%\%%~nxe"
)
GOTO :EOF
Always verify against a test directory before applying to real data.
Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around %filename1% can be omitted.
This should accomplish the task. It simply reads the first line from each file, removing leading spaces (one of the default delims) and sends it to the output, resetting the first flag to prevent regurgitation of the remaining data.
Next step is to reproduce all but the first line.
All gathered together by enclosing the two fors in parentheses and redirecting.
Note that the source and destination directories must be different.
I have a file as "RAAAAAAV.KKK9.Z01_YYYYMMDDhhmmss". I want to remove the last part after the underscore"_" using a one liner dos command. Please help.
Output required:
RAAAAAAV.KKK9.Z01
The following works
#echo off
set var=RAAAAAAV.KKK9.Z01_YYYYMMDDhhmmss
set "var=%var:_="&rem %
set var
To rename a specific file:
ren "RAAAAAAV.KKK9.Z01_20151009231015" *.Z01
To rename all files with extension like Z01_timestamp:
ren *.Z01_?????????????? *.Z01
To rename all files where the beginning of the extension is unknown:
for %F in (*.???_??????????????) do #for /f "delims=_" %X in ("%~xF") do #ren "%F" "%~nF%X"
If used in a batch script, then percents must be doubled:
#echo off
for %%F in (*.???_??????????????) do for /f "delims=_" %%X in ("%%~xF") do ren "%%F" "%%~nF%%X"
EDIT - 2015-10-10
If you really want to have precise control over which files get renamed, then you can use my JREN.BAT regular expression renaming utility - a hybrid JScript/batch script that runs natively on any Windows machine from XP onward. The following simple one liner strips off the underscore and timestamp from any file that has an extension consisting of any combination of letters and digits, followed by an underscore, followed by a 14 digit timestamp.
jren "(\.[A-Z0-9]+)_\d{14}$" $1 /i
Maybe already late, but I will share my founding anyway for benefit to others.
You can use batch sub-procedure/function
:leftStr STRING SEPARATOR RESULT_VAR
::get left string before some specific SEPARATOR
set "STRING=%~1" get arg1
set "RIGHT=%STRING:*%~2=%" anything after arg2
set "%~3=!STRING:%~2%RIGHT%=!" strip that substr
exit /b
Then you can call it from other places, works even inside for loop with delayedExpansion enabled.
..
call :leftStr "left_right" "_" varname
echo !varname!
#rem varname = left
note: this is not a fancy function, no error checking or such. You'd better make sure your input string, separator, return_var is already valid or no conflict with any other.
EDIT: forgot to mention, this is for arbitrary length string and separator, for a simple, fixed length you can just use %var:~-N1,N2% (with negative N1)
I am running a batch file on Windows 7 and running into this error (I have narrowed down the error to the following line):
FOR /F "delims=" %%I in ('echo %RegVal%') do set sasroot=%%~sI
Where Regval is the file path of a given software, which in this case (on my Win7 machine) is:
RegVal = C:\Program Files\SAS 9.2_M3_10w37\SASFoundation\9.2(32-bit)
This same script used to work on Windows Vista, although I suspect it may be that there a parenthesis in RegVal now as it was previousy C :\Program Files\SAS 9.2_M3_10w37\SASFoundation\ on my previous Vista machine.
You suspection is correct.
To get around it, enclose your variable into doublequotes (You remove them again with the ~ in the setcommand)
FOR /F "delims=" %%I in ('echo "%RegVal%"') do set sasroot=%%~sI
I suggest you create a file with the value of RegVal in it, then parse it using the FOR loop:
echo %RegVal%>C:\SomeFile.txt
FOR /F "delims=" %%I in (C:\SomeFile.txt) do set sasroot=%%~sI
This should help you get around your problem.
Stephan's solution is much simpler, but I'll explain my solution anyway, which might prove useful in some cases.
When the FOR command parses the data specified in the IN part using a command, it replaces the command with the result of the command, then runs the FOR command. For example, with the question above, the FOR command that will be executed after expanding echo %RegVal% is:
FOR /F "delims=" %%I in (C:\Program Files\SAS 9.2_M3_10w37\SASFoundation\9.2(32-bit)) do set sasroot=%%~sI
Thus, when the parser hits the first closing parenthesis, it will stop, thinking that everything it read before is the text to work on. However, in this case this is wrong, as the first closing parenthesis is part of the string to read; it doesn't indicate the end of the string.
When parsing a file with the FOR command, it will read each line, assign the predefined tokens with the correct values, then execute the code block that follows. Rinse and repeat for every line in the file. But in this case, it will not replace the IN part with each line; it will only parse it and assign values to the tokens. This is the reason why special characters (such as parenthesis) do not create parsing errors in this case.
My problem is pretty simple, but I'm unsure why I am seeing this behaviour. I want to get a list of parameters down to a single entry at a time so I can do some processing on them. The list of jar files I am processing is separated by a ; delimiter.
set JARS=this.jar;that.jar;and.jar;the.jar;other.jar
for /f "delims=;" %%a in ("%JARS%") do echo.%%a
I'm expecting the script to exit listing as follows.
this.jar
that.jar
and.jar
the.jar
other.jar
C:\>
But the script is instead exiting as follows.
this.jar
C:\>
I'm clearly missing something obvious, but I can't seem to see it.
I'm using Windows 7.
Try this: The semicolon is a separator on a command line so it will delimit the filenames.
set JARS=this.jar;that.jar;and.jar;the.jar;other.jar
for %%a in (%JARS%) do echo.%%a
I have a list of 4000 .tif images that I need copied from a folder containing 20,000+ images.
I am trying to use command prompt to do so by using code found from googling my problem.
the code is as follows:
for /f %a in H:\list.txt do copy %a H:\new
"H:\list.txt" is my list file
"H:\new" is where i want my files to be copied to
I am running this command in the file folder that contains the .tif files H:\Doc2 and I keep getting:
H:\list.txt was unexpected at this time.
What is causing the issue? Is my list not correctly set up?
It looks like this:
ABBA.TIF
ABBD.TIF
ABBQ.TIF
Do i need commas or colons after the file names?
H:\list.txt is a literal, you want to loop over each of the lines of the file. Check this out:
How do you loop through each line in a text file using a windows batch file?
for /F "tokens=*" %%A in (myfile.txt) do [process] %%A
Suggestion: tag this with "windows" as well.
Amir diagnosed your immediate problem - your code is missing the parentheses around the IN clause.
But you have another potential problem. Your list may include file names and or paths that contain spaces or other special characters. If so, then the name must be quoted.
The values in the list may already by quoted, or they may not. You can use the ~ modifier to strip any existing enclosing quotes that may or may not be there, and then explicitly add your own.
for /f %a in (H:\list.txt) do copy "%~a" H:\new
If you want to include the line in a batch file, then each % must be doubled as %%.