pleas help.
SET "prog=C:\Program Files (x86)\Common Files\Adobe"
SET "file=%prog%\Color\Profiles\%Value%"
FOR /f %%i IN ("%file%") DO (
ECHO filename=%%~ni
ECHO path=%%~fi
pause
)
Result:
filename=Program
path=C:/Program
I need path=C:\Program Files (x86)\Common Files\Adobe\Color\Profiles\%Value%
and filename %Value% no Program...
Thx.
The FOR /F command as you wrote it is designed to process A STRING enclosed in quotes. This way, the following command:
FOR /f %%i IN ("%file%") DO (
place in %%i variable the first token of such string ("C:\Program") and %%~ni tries to extract a file name from such string!
If you want to extract the name of the file, you should use a plain FOR command with NO /F option:
FOR %%i IN ("%file%") DO (
ECHO filename=%%~ni
ECHO path=%%~fi
pause
)
What I tried to mean is that in this case you want to process the %file% variable contents as a file name, NOT as a string, so you should use a plain FOR command designed to process files (instead FOR /F designed to process strings).
If you change your FOR line to this, you'll get results closer to what you want:
FOR /f "DELIMS=" %%i IN ("%file%") DO (
OR
FOR /f "TOKENS=*" %%i IN ("%file%") DO (
The specified TOKENS and DELIMS parsing options allow the FOR to accept the whole string from %file% into %i and ignore spaces. Source: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true
Related
I want to read a .txt file using batch script.
Each line should be stored in a variable.
My problem: I have to give the command a file path to the .txt file. Unfortunately this hasn't worked so far. The solution is probably very simple, but I haven't found it yet.
for /f "tokens=*" %%a in ("%FilePath%backup\packs.txt") do (
Set /a count+=1
Set url[!count!]=%%a
)
echo %url[2]%
for /f loops can process three different types of data - files, strings, and commands. Each of these is indicated differently when you call the for command.
A file is processed by not using quotes in the set area: for /f %%A in (file.txt) do (
A command is processed by using single quotes: for /f %%A in ('find "abc" file.txt') do (
A string is processed by using double quotes: for /f %%A in ("hello world") do (
Of course, sometimes you need to process a file with a space in the path, and that's when you'd use the usebackq option. This option will still process all three types of data, but the indicators will be different.
A file is processed by using double quotes: for /f "usebackq" %%A in ("some file.txt") do (
A command is processed by using backticks: for /f "usebackq" %%A in (`find "don't" file.txt`) do (
A string is processed by using single quotes: for /f "usebackq" %%A in ('19^" screen') do (
Either removing the quotes from your file path or adding the usebackq option will set the variables for you.
#echo off
setlocal enabledelayedexpansion
set "FilePath=.\test_path\"
set "count=0"
for /f "usebackq tokens=*" %%A in ("%FilePath%backup\packs.txt") do (
set /a count+=1
set "url[!count!]=%%A"
)
echo %url[2]%
I want to loop through a folder and let run an algorithm on each .tif file found. Unfortunately this does not work for files which have a space character in their name. As my path already contains folders with space, i put the variable which stores the path name in double-quotation marks.
FOR /F %%k IN ('DIR /B "%mypath_import%"*.tif') DO (
SET infile=%%k
SET outfile=!infile:.tif=_UTM.tif!
REM ... do something
This is my attempt so far but it won't work for the files which include a space as well.
You done need all that. You can use the normal for loop without having to use /f
#echo off
setlocal enabledelayedexpansion
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do (
set "infile=%%~i"
echo "!infile:.tif=UTM.tif!"
)
The above will however echo full path to and file name, if you want filename only with extension:
#echo off
setlocal enabledelayedexpansion
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do (
set "infile=%%~nxi"
echo "!infile:.tif=UTM.tif!"
)
or without the need to delayedexpansion
#echo off
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do echo %%~dpni_UTM%%~xi
and again if you require the name and extension only.
#echo off
set "mypath_import=C:\Some path"
for %%i in ("%mypath_import%*.tif") do echo %%~ni_UTM%%~xi
EDIT
As per comment from #Stephan, keep in mind if you are doing actual renames and you run the script more than once it will keep on appending _UTM each time. So you'll get filename_UTM_UTM.tif etc. So you can exclude files from the loop by including findstr
for /f "delims=" %%i in ('dir /b *.tif ^|findstr /eiv "_UTM.tif"') do echo %%~ni_UTM%%~xi
I'm having a problem to read a file in the batch script and then set the first word of that file into a variable, and then I'll use it later.
My code is:
set /p file= | dir /b .\*.txt
for /f "delims=-" %%i in (%file%) do set %db_sid%=%%i
pause
I need to read the file and set just the first word as a db_sid variable.
This is the current content of the text:
prod11i-sarasa
I want db_sid to be just prod11i.
At first you need to get the first line of the text file, which is done by set /p var=<file. Then you can split the string with a for /f (or alternatively cut the second part off with string manipulation, but since the * is only supported as first character, you would need to make that twice using a temporary variable).
for /f "tokens=* delims=" %%F in ('dir/b *.txt') do set /P db_sid=<"%%~fF"
for /f "tokens=1 delims=-" %%W in ("%db_sid%") do set "db_sid=%%W"
If you want the first word of the first file you'll need to exit the for with a goto.
:: Q:\Test\2019\06\06\SO_5640691.cmd
#Echo on
for %%F in (*.txt) do for /f "usebackq delims=-" %%W in ("%%F") do set "db_sid=%%W"&goto :Out
:Out
Set db_sid
This information below is contained in a text file and formatted as such.
/var/www/xxx/html/videos/video_folder_1
/var/www/xxx/html/videos/video_folder_2
/var/www/xxx/html/videos/video_folder_3
/var/www/xxx/html/videos/video_folder_4
/var/www/xxx/html/videos/video_folder_5
/var/www/xxx/html/videos/video_folder_6
/var/www/xxx/html/videos/video_folder_7
I also have a variable called %file_name% in the batch file already defined.
So lets say that is it is %file_name% = V001-video_folder_6.mp4
As you can see there is some more extra information, V001- and .mp4.
I would like to use the var %file_name% to search the text file and return the entire line. In this case it would return /var/www/xxx/html/videos/video_folder_6 and then put this information in a new var, let us say, %folder_path%.
I think I would use findstr however I have been playing around and not getting the best results.
The problem with the methods that use findstr is that they are slow, because they require to execute findstr.exe (a ~30KB file) each time. A simpler/faster solution is to use just internal Batch commands with the aid of an array. If the number of names to process is large, the difference in time between the two methods may be marked.
#echo off
setlocal EnableDelayedExpansion
rem Load the lines from text file into an array with the last part as index:
for /F "delims=" %%a in (test.txt) do (
set "line=%%a"
for %%b in (!line:/^= !) do set "lastPart=%%b"
set "folder[!lastPart!]=%%a"
)
set "file_name=V001-video_folder_6.mp4"
rem Get the folder from file_name:
for /F "tokens=2 delims=-." %%a in ("%file_name%") do set "folder_path=!folder[%%a]!"
echo Folder path is: %folder_path%
Let us assume the posted lines are in file Test.txt in current working directory.
#echo off
set "file_name=V001-video_folder_6.mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto NextCommand
)
:NextCommand
echo Full folder path is: %folder_path%
Open a command prompt window, enter the command for /?, hit key RETURN or ENTER and read output help to understand this little code.
The command goto inside FOR loop results in an immediate exit from loop processing output of findstr.exe after first found line containing the folder path of interest.
Perhaps better in case of searched folder is not found in text file:
#echo off
set "file_name=V01-VIDEOS for school (Miss Patrick).mp4"
for /F "tokens=2 delims=-." %%A in ("%file_name%") do set "folder=%%A"
for /F "delims=" %%P in ('%SystemRoot%\System32\findstr.exe "/C:%folder%" Test.txt') do (
set "folder_path=%%P"
goto FoundFolder
)
echo "%folder%" not found in file Test.txt.
pause
goto :EOF
:FoundFolder
echo Full folder path is: "%folder_path%"
pause
This should work:
::file_name=V001-video_folder_6.mp4
::file containing folder paths is called paths.txt
for /f "tokens=2 delims=-." %%a in ("%file_name%") do set FN=%%a
for /f %%a in ('findstr /E /L "%FN%" "paths.txt"') do set folder_path=%%a
echo %folder_path%
Which does what you want in effectively two lines.
I have a simply FOR /F loop which strips out all but one line of a text file:
for /f "skip=12 tokens=* delims= " %%f in (.\NonProcessed\*.txt) do (
> newfile.txt echo.%%f
goto :eof
)
But when I run, I get the result:
The system cannot find the file .\NonProcessed\*.txt
The for loop works fine if I enter a fully qualified path to the text file within the brackets, but it can't handle the relative link I have in there. I've been able to use the exact same relative link in another standard for loop in a different batch file running in the same directory without any issues. I can't understand why it won't work! Please help.
EDIT: For comments, code I'm using now is
for %%f in (.\NonProcessed\*.txt) do (
echo f is %%f
for /f "usebackq skip=12 tokens=* delims= " %%a in (%%f) do (
echo a is %%a
> %%f echo.%%a
goto :continue
)
:continue
sqlcmd stuff here
)
Sorry but for /f does not allow you to do that. And no, the problem is not the relative path to files but the wildcard.
According to documentation, you have the syntax case
for /F ["ParsingKeywords"] {%% | %}variable in (filenameset) do command [CommandLineOptions]
For this case, documentation states The Set argument specifies one or more file names. You can do
for /f %%a in (file1.txt file2.txt file3.txt) do ...
but wildcards are not allowed.
If you don't know the name of the file you want to process, your best option is to add an additional for command to first select the file
for %%a in (".\NonProcessed\*.txt"
) do for /f "usebackq skip=12 tokens=* delims= " %%f in ("%%~fa"
) do (
> newfile.txt echo(%%f
goto :eof
)
When executed, the goto command will cancel both for loops so you end with the same behaviour you expected from your original code.
edited to adapt code to comments
#echo off
set "folder=.\NonProcessed"
pushd "%folder%"
for /f "tokens=1,2,* delims=:" %%a in (
' findstr /n "^" *.txt ^| findstr /r /b /c:"[^:]*:13:" '
) do (
echo Overwrite file "%%a" with content "%%c"
>"%%a" echo(%%c
)
popd
Read all the files in the folder, numbering the lines. The output for the first findstr command will be
filename.txt:99:lineContents
This output is parsed to find the line 13, the resulting data is splitted using the colon as a separator, so we will end with the file name in %%a, the line number in %%b and the line content in %%c.
SET FILES_LIST=files_list.config
DIR /b .\NonProcessed\*.txt>!FILES_LIST!
for /f "skip=12 tokens=* delims= " %%f in (!FILES_LIST!) do (
> newfile.txt echo.%%f
goto :eof
)
IF EXIST "!FILES_LIST!" DEL "!FILES_LIST!"
I did not check how your's FOR works, just added my additions/corrections to it.... Hope it will work for you.
Best regards!