Program have 2 argument 1-folder name 2- file name and if 2- argument equal to txt file name in current directory echo message else copy all txt files to folder given as first argument
This how i try
#echo off
FOR %%f in (*.txt) do ( IF %%f==%2 ( ECHO PARAM 2 IS NOT COPIED ) ELSE xcopy %%f "%1\" )
)
FOR %%f in ("%1\*.txt") do echo %%f>>"%1\logcopy.txt"
in result program cout message and copy all files to folder
for %%f in (*.txt) do if /i "%%~ff"=="%~f2" (
echo "%~2" is not copied
) else (
xcopy /i "%%~ff" "%~1\."
)
for each .txt file (%%f), if its name and extension (%%~nxf) match the second argument (%~2 is the second argument without quotes, if present) echo message, else, xcopy the file (with full path) to the target folder indicated as first argument
Related
I have a text file which contains the following two lines:
-host MYPC -param 3 -param2 4
-host MYPC -param3 2 -param4 5
I want to get the value corresponding to -param which is 3 (-param 3) in a Windows batch file.
I tried that with the findstr command as shown below:
findstr /R "^-param$" > step1_a.txt my_file.txt
But it output this:
FINDSTR: /c ignored
FINDSTR: /A ignored
FINDSTR: /f ignored
FINDSTR: /f ignored
FINDSTR: /t ignored
FINDSTR: /y ignored
FINDSTR: /a ignored
FINDSTR: /k ignored
I think, this is caused by - in the string.
How to get the value 3 associated with -param in my text file?
Here is an example code on how to process not really fail safe, but hopefully good enough for your purpose, the lines read from a text file to get the next argument string after a well-known argument string like -param.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "DataFile=%~dp0my_file.txt"
rem Does the input data file exist?
if exist "%DataFile%" goto ProcessData
rem Input data file not found in directory of the batch file.
echo ERROR: Could not find file: "%DataFile%"
goto :EOF
:ProcessData
set "ParamValue="
for /F usebackq^ delims^=^ eol^= %%I in ("%DataFile%") do for %%J in (%%I) do (
if not defined ParamValue (
if /I "%%~J" == "-param" set "ParamValue=1"
) else (set "ParamValue=%%~J" & goto HaveValue)
)
rem The parameter of interest was not found at all or there is no value.
echo ERROR: Could not find the parameter with name: "-param"
goto :EOF
:HaveValue
rem Output the parameter value as an example for further command lines.
set ParamValue
endlocal
The outer FOR loop reads non-empty lines one after the other from text file and assigns each line completely to the specified loop variable I.
The inner FOR loop processes the current line similar to how cmd.exe processes the argument strings passed to a batch file. All space/tab/comma/semicolon/equal sign/non-breaking space (in OEM encoding) delimited strings are ignored until a string is found which is case-insensitive equal the string -param. The next string in the current line is assigned to the environment variable ParamValue and the two loops are exited with the command GOTO to continue batch file processing on the line below the label :HaveValue where the environment variable ParamValue can be used for whatever purpose.
This extended version of above gets first the string after -param which is in the example 3. Then the entire text file is searched again for an argument string starting with -param and the string appended which was read first from file which is in the example -param3. If this string is found, the next string is assigned to environment variable ParaValue which is 2 in the example.
#echo off
set "DataFile=%~dp0my_file.txt"
rem Does the input data file exist?
if exist "%DataFile%" goto ProcessData
rem Input data file not found in directory of the batch file.
echo ERROR: Could not find file: "%DataFile%"
goto :EOF
:ProcessData
set "ParamName="
for /F usebackq^ delims^=^ eol^= %%I in ("%DataFile%") do for %%J in (%%I) do (
if not defined ParamName (
if /I "%%~J" == "-param" set "ParamName=1"
) else (set "ParamName=-param%%~J" & goto HaveName)
)
rem The parameter of interest was not found at all or there is no value.
echo ERROR: Could not find the parameter with name: "-param"
goto :EOF
:HaveName
set "ParamValue="
for /F usebackq^ delims^=^ eol^= %%I in ("%DataFile%") do for %%J in (%%I) do (
if not defined ParamValue (
if /I "%%~J" == "%ParamName%" set "ParamValue=1"
) else (set "ParamValue=%%~J" & goto HaveValue)
)
rem The parameter of interest was not found at all or there is no value.
echo ERROR: Could not find the parameter with name: "%ParamName%"
goto :EOF
:HaveValue
rem Output the parameter value as an example for further command lines.
set ParamValue
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~dp0 ... batch file path ending always with a backslash.
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
You used anchors ^ and $ which mark begining of a line and end of a line (see findstr docs).
Correct pattern would be: findstr /r /c:"-param [0-9]*"
I used additional flag /c, so you can use spaces inside the pattern and they will not be treated as "or" operator.
Pattern is simple: first match -param literally, then match zero or more digis with [0-9]*
With a bat file I sorted the subfolders list in a Directory but what are the 2 folders named "." 1 dot and ".." 2 dots ? actually they are not present in windows explorer and most of all how I can avoid to show them in the list ?
#echo off
setlocal EnableExtensions EnableDelayedExpansion
> ".\Utils\Check last modified Profile.txt" (
for /F "delims=" %%D in ('
dir %APPDATA%\Mozilla\Firefox\Profiles\ /A /all-D /TW /A:D /O:-DE
') do (
rem print each item:
echo %%~D %
)
)
endlocal
image here > https://imgur.com/8XyQk6q
dir %APPDATA%\Mozilla\Firefox\Profiles\ /A /all-D /TW /A:D /O:-DE ^|findstr /v /e /L /c:"."
worked for me.
The dir output is sent to findstr which allows through lines which do not (/v) end (/e) with the literal (/L) string "."
Directories cannot be created that end with .
I believe I do not need to comment further on the attributes-selection specification.
The ^ is required to have the pipe applied to the dir command, not the for.
I'll make no further comment on the attributes-selection specified.
. is the current directory. .. is it's parent directory.
Explorer hides them.
Dir C:\windows\.\.\..
I have decided to post this because the existing answers appear to be using the same crazy and incorrect options for the Dir command. The following examples use the appropriate findstr.exe method of eliminating the . and .. directory entries from your listing, sorted according to most recently modified to least recent. I have included the full path to findstr.exe to eliminate the possibility of %PATH% and/or %PATHEXT% modifications from not being able to locate it.
If you wanted the same output format, then this should work for you:
#For /F "Tokens=1*Delims=:" %%G In ('"Dir "%AppData%\Mozilla\Firefox\Profiles" /AD/O-DE 2>NUL|%SystemRoot%\System32\findstr.exe /ELNV ".""')Do #Echo(%%H
If you don't need the empty lines then, this should omit those for you:
#For /F "Delims=" %%G In ('"Dir "%AppData%\Mozilla\Firefox\Profiles" /AD/O-DE 2>NUL|%SystemRoot%\System32\findstr.exe /ELV ".""')Do #Echo %%G
And if you only needed the directory name lines, then perhaps this will satisfy you:
#For /F EOL^=^ Delims^= %%G In ('"Dir "%AppData%\Mozilla\Firefox\Profiles" /AD/O-DE 2>NUL|%SystemRoot%\System32\findstr.exe /ELV ".""')Do #Echo %%G
You can use and add this switch /B
#echo off
#for /F "delims=" %%D in ('
dir %APPDATA%\Mozilla\Firefox\Profiles\ /A /all-D /TW /A:D /O:-DE /B
') do (
echo %%~D
)
I have a set of folders with a txt file of the same name in each one. They're path is
C:\Test\Salford_xxx\MenuSettings.txt
C:\Test\Salford_xxx\MenuSettings.txt
C:\Test\Salford_xxx\MenuSettings.txt
Where xxx is a random 3 digit number. I want the 1st line of each of these files to be changed using a text file called input.txt, which has the path and the line thats replacing the 1st line of each file. It looks like this.
C:\TEST\SALFORD_001\MENUSETTINGS.TXT
AppName: "This needs replacing 1"
C:\TEST\SALFORD_011\MENUSETTINGS.TXT
AppName: "This needs replacing 2"
C:\TEST\SALFORD_345\MENUSETTINGS.TXT
AppName: "This needs replacing 3"
C:\TEST\SALFORD_761\MENUSETTINGS.TXT
AppName: "This needs replacing 4"
C:\TEST\SALFORD_768\MENUSETTINGS.TXT
AppName: "This needs replacing 5"
C:\TEST\SALFORD_999\MENUSETTINGS.TXT
AppName: "This needs replacing 6"
I've written a for loop that puts the path and the replacement in variables, which works:
for /F "delims=" %%a in ('findstr /R /I "Salford" Input.txt') do (set "FilePath=%%a" echo %FilePath%)
for /F "delims=" %%b in ('findstr /R /I "AppName" Input.txt') do (set "NewName=%%b" echo %NewName%)
AppName is a word that is always in the first line, so that is used to search.
Here is the script for replacing the line of each file.
set "search=Appname"
set "replace=%NewName%"
set "newfile=NewOutput.txt"
(for /f "delims=" %%i in (%FilePath%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
move "%newfile%" "%FilePath%"
However the loop continues to the last item in the Salford_999 folder and just edits that file. How can i make this read the first 2 lines of input.txt, make the replacement and then loop to the next two lines and so on?
Thanks
I don't understand how your code use your specifications. For example, your "for loop that puts the path in a variable" assign all paths to the same variable, so when the for ends, the variable have the last value. Also, I don't understand where the "AppName" string literal is taken from when it is used in the findstr in the second for. Finally, what happen if there is a file that have not its corresponding line in input.txt file?
A different approach is to process the input.txt file as a series of odd/even lines, and then process each one of the files described in odd lines; this may lead to a simpler solution:
#echo off
setlocal EnableDelayedExpansion
rem Process odd and even lines from input.txt file:
set "FilePath="
for /F "delims=" %%a in (input.txt) do (
if not defined FilePath ( rem Odd line...
rem Assign each odd line to "FilePath" variable
set "FilePath=%%a"
) else ( rem Even line...
rem Process this file, if it exists
if exist "!FilePath!" (
rem Block to enclose output to new file
> newfile (
rem The first line in new file is this even line in input.txt file
echo %%a
rem Copy all lines from this file, excepting the first one
more +1 "!FilePath!"
)
move /Y newfile "!FilePath!"
)
set "FilePath="
)
)
Thanks in Advance.
Using a DOS batch file, I am trying to read a text file that contains several full paths with quotes, separated by a space and write a new file containing one path per line.
For example, I want to turn this file:
"C:\path\filename.doc" "C:\path\filename.doc" "C:\path\filename.doc" "C:\path\filename.doc"
into this:
"C:\path\filename.doc"
"C:\path\filename.doc"
"C:\path\filename.doc"
"C:\path\filename.doc"
I have had some success using the wonderful repl.bat (by dbenham).
type "files.txt" | repl " " "\r\n" x l >"newfile.txt"
But when there are spaces in the filenames or path it breaks a new line in the middle of the path and wrecks it.
Ive tried passing as the search variable into repl using the escape character ^, i.e. repl "^" ^"" and other ways with no joy.
At the end of the day, I simply need to move all the files into another directory, and so was going to then pass the resulting text file to another bulk delete batch file for processing, but perhaps there is a better way im missing ?
This has a limitation in the length of the line, of around 8 KB.
Less than that and it will move the files to your new folder.
#echo off
for /f "usebackq delims=" %%a in ("c:\folder\file.txt") do (
for %%b in (%%a) do move "%%~b" "d:\existing\new\folder"
)
The code below should work to move all files in except the ones in the list.
It adds a hidden attribute to the files in the list, moves all the other files, then removes the hidden attributes again.
#echo off
for /f "usebackq delims=" %%a in ("c:\folder\file.txt") do (
for %%b in (%%a) do attrib +h "%%~b"
)
cd /d "c:\folder"
move *.* "d:\already\existing folder"
for /f "usebackq delims=" %%a in ("c:\folder\file.txt") do (
for %%b in (%%a) do attrib -h "%%~b"
)
Test code for Windows 2012 as mentioned in the comments
#echo off
(echo "c:\widget1\test 1.txt" "2:\widget2\test 2.doc")>"file.txt"
for /f "usebackq delims=" %%a in ("file.txt") do (
for %%b in (%%a) do echo move "%%~b" "d:\existing\new\folder"
)
pause
You could use the following batch file split.bat and call it redirecting the content of your text file into it and redirecting the output into another file like split.bat < files.txt > newfiles.txt:
#echo off
set /P INFILE=
call :SPLIT %INFILE%
exit /B
:SPLIT
shift
if "%~0"=="" exit /B
echo "%~0"
goto :SPLIT
If you do not provide an input file (< files.txt) the scripts prompts you for a space-separated list.
If no output file is given (> newfiles.txt), the created new-line-separated list is shown on screen.
Notice that this does not verify whether your input file fulfills the described formatting.
This method is limited to a list length of 1021 bytes (characters), everything after will be truncated!
Assuming you can guarantee that each file path is enclosed within double quotes, then you just need to tweak your REPL.BAT command a bit:
type "files.txt" | repl "(\q.*?\q) *" "$1\r\n" x >"newfile.txt"
But REPL.BAT has been superseded by JREPL.BAT - it has even more functionality, and a slightly different syntax.
A JREPL solution can be as simple as:
jrepl "\q.*?\q" $0 /x /jmatch /f file.txt /o newfile.txt
If you want, you can overwrite the original file with the result by specifying - as the output file.
jrepl "\q.*?\q" $0 /x /jmatch /f file.txt /o -
If each line in the original file is <8k, then the following pure batch script should work, and it is pretty simple:
#echo off
>newfile.txt (
for /f "delims=" %%A in (files.txt) do for %%B in (%%A) do echo %%B
)
I have a list in a text file.
Using a batch file, I need to search for references of each item in the list.
I will need to be able to determine where the item is referenced.
Here is a what I have tried:
REM WINDOWS COMPILE FORMS
cls
#echo off
for %%f IN (LIST.TXT) do
findstr /m "$item_name$" *.* > $item_name_$ || results.txt
if %errorlevel%==0 (
echo Found! logged files into results.txt
) else (
echo No matches found
)
pause
My problem is I cannot find a way to plug the item in my text list into the batch file.
you can achieve the results you want just using findstr command. Read HELP FINDSTR and then try
findstr /m /F:txt.lst "$item_name$"
Asuming that the elements on the list can be used as file names
for /f "tokens=*" %%f IN (LIST.TXT) do (
findstr /m /l /c:"%%f" *.* > "%%f"
)
For each element in the list, search the files for the element and output the list of files containing it to a file named as the element in the list