Windows Batch job to precede all lines with filename - windows

I have 50 text files and each text file has around 6000 lines. I am looking to make a batch job that will append the filename to each line of the text file.
I have a batch job to append data to the each line but cant wrap my head around getting the filename
This is what I have so far
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (input.txt) do (
set /a N+=1
echo ^"(my filename)%%a^",>>output.txt
)
--- Example
filename 3315.txt
124123541234
1234123
2345623462356234
12341234562356245
Desired end result
3315124123541234
33151234123
33152345623462356234
331512341234562356245

#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\R*.txt" '
) DO (
(
FOR /f "usebackqdelims=" %%q IN ("%sourcedir%\%%a") DO (
ECHO (%%a^)%%q
)
)>"%destdir%\%%~na.out"
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a filemask of R*.txt to restrict the number of files processed on my test system.
Essentially, build a list of filenames using the dir command, and assign each to %%a in turn.
read each line of %%a into %%q and output to a new file in the destination directory made up of the name part of %%a (%%~na) and .out
--- later
To prepend the name part of the file to each line of the file, change
ECHO (%%a^)%%q
to
ECHO %%~na%%q
%%~na selects the Name part of %%a (see for /f|more from the prompt for more info)

Related

How to rename Windows files using extracted substring from current names

I have a directory of academic papers that were named using the convention below:
Author1-(Year)-Title.pdf
For example,
Jones-(2011)-XXX.pdf
Smith-(2002)-YYY.pdf
Johnson-(2015)-ZZZ.pdf
I would like to rename them as
(2011)-Jones-XXX.pdf
(2002)-Smith-YYY.pdf
(2015)-Johnson-ZZZ.pdf
That is, to extract the year from the file name and put it in front.
I tried the following code, which did not work
Setlocal enabledelayedexpansion
Set "Year=2013"
Set "Replace="""
For %%a in (*.pdf) Do (
Set "NewName=(%year%)-%%~a"
Ren "%%a" "%NewName%-File:%Year%=%Replace%!"
)
Pause&Exit
In case XXX also contains hyphens I'd suggest using tokens=1,2* to stop parsing the remainder of the file name.
I'd also remove the parentheses, when the year is first place there is no need to further emphasize it.
#Echo off
for /f "tokens=1-2* delims=-()" %%A in (
'Dir /b "*-(*)-*.pdf"'
) do Ren "%%A-(%%B)-%%C" "%%B-%%A-%%C"
Sample output
> dir /b
2002-Smith-YYY.pdf
2011-Jones-XXX.pdf
2015-Johnson-ZZZ.pdf
Not tested
for /f "tokens=1,2,3 delims=-" %%a in ('dir /b "*.pdf"') do (
echo ren "%%a-%%b-%%c" "%%b-%%a-%%c"
)
this will only echo the intended rename command.If it looks ok remove the echo word.
Derived form this SO article - it works:
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%x IN (*.pdf) DO (
FOR /f "tokens=1-3 delims=-" %%a IN ("%%~x") DO (
SET "Author=%%a"
SET "Year=%%b"
SET "Title=%%c"
ren %%x !Year!-!Author!-!Title!
)
)
Here is a reliable way of doing what you are asking for even if the author part contains - on its own. The title portion may even contain ( and ), but the author part must not. So this is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILES=.\*-(????)-*.pdf" & rem // (basic pattern to match correct files)
set "_REGEX=^[^()][^()]*-([0123456789][0123456789][0123456789][0123456789])-.*\.pdf$" ^
& rem // (precise filter to exclude files violating the demanded pattern)
if not defined _REGEX set "_REGEX=.*" & rem // (avoid trouble with empty filter)
rem // Loop through all matching files:
for /F "eol=: tokens=1,2,* delims=()" %%F in ('
dir /B /A:-D "%_FILES%" ^| findstr /I /R /C:"%_REGEX%"
') do (
rem // Store the extracted file name parts:
set "LEFT=%%F" & set "YEAR=%%G" & set "REST=%%H"
setlocal EnableDelayedExpansion
rem // Reassemble the name and rename the file:
ECHO ren "!LEFT!(!YEAR!)!REST!" "(!YEAR!)-!LEFT!!REST:*-=!"
endlocal
)
endlocal
exit /B
After having verified the correct output, remove the upper-case ECHO command to actually rename files.

Parse and Rename text files

I need to rename all of these files to the 6 character Part Number(306391) on the 3rd line.
Currently i have:
setlocal enabledelayedexpansion
set first=1
for /f "skip=3 delims= " %%a in (Name.txt) do (
if !first! ==1 (
set first=0
echo %%a > out.txt
ren Name.txt %%a.txt
)
)
Which finds the 6 digit part number and renames the file to the correct name. But breaks if i use *.txt instead of the actual name of the .txt file. I need it to work for all .txt files in the directory.
Surround your for /f loop with another for loop, then reference the outer loop variable in your ren command. You can also eliminate the need for delayed expansion by using if defined for boolean checks. I put in other tweaks here and there. Just ask if you want details.
#echo off
setlocal
for %%I in (*.txt) do (
set first=
for /f "usebackq skip=3" %%a in ("%%~fI") do (
if not defined first (
set first=1
echo %%~nxI ^> %%a.txt
rem // uncomment this when you're satisfied that it works correctly
rem // ren "%%~fI" "%%a.txt"
)
)
)
This method should run faster, specially if the files are large, because just 4 lines of each file are read (instead of the whole file):
#echo off
setlocal EnableDelayedExpansion
rem Process all .txt files
for %%f in (*.txt) do (
rem Read the 4th line
(for /L %%i in (1,1,4) do set /P "line4=") < "%%f"
rem Rename the file
for /F %%a in ("!line4!") do ECHO ren "%%f" "%%a.txt"
)

set values by file names in a directory

Hi I want to set value InFile for all text files in a directory which means that I want the batch file to load and do the command for all of the text files in the directory one by one.
Normally I just copy the command how many times files are then I replace Infile with every file name. By this code I get the file names and then I replace them.
cd /d "dir"
dir /a /b /-p /o:gen >names.txt
and here's the example of a command.
#Echo OFF
REM Set These Variables
SET "InFile=123.txt"
SET "OutFile=NowLoad.txt"
SET "Replace=KK"
SET "ReplaceWith=JJ"
REM Get Total Lines Number [including empty lines]
FOR /F %%A IN ('TYPE "%InFile%"^|find /v /c ""') DO SET "Till=%%A"
REM Create The OutFile with changes
SETLOCAL EnableDelayedExpansion
<"!InFile!" (
FOR /L %%a IN (1 1 0) DO SET /p "="
FOR /L %%A IN (1 1 %Till%) DO (
SET "line="
SET /P "line="
IF "!line!x" == "x" ( Echo.
) ELSE ( Echo !line:%Replace%=%ReplaceWith%!)
)
)>>"%OutFile%"
ENDLOCAL
no need to count the lines, just process every line:
setlocal enabledelayedexpansion
for %%f in (*.txt) do (
( for /f "delims=" %%A in (%%f) do (
set line=%%A
set line=!line:%Replace%=%ReplaceWith%!
echo(!line!
)
)>%%~nf.out
)
Note: the ( after echo handles empty lines (an empty line is written with no "echo is off" if !line! is empty)
edited to process all .txt files in the Folder. Because a fixed %oufile% would overwrite the same outfile with every processed infile, I changed it to a new Extension: <SameNameAsInputFile>.out
Another way...
Your script is DOIT.bat.
Change DOIT.bat to use a command line parameter for the filename you want to process.
#Echo OFF
REM Set These Variables
SET "InFile=%~1"
SET "OutFile=NowLoad.txt"
...
Create another file named CALLIT.bat
#ECHO OFF
FOR /F "usebackq tokens=*" %%f IN (`dir /b *.txt`) DO (
CALL DOIT.bat "%%~f"
)
Running CALLIT.bat will process every .txt file in the directory.

Insert Into Multiple Files in Windows shell script

Basically I'm trying to write a batch file to insert some code into multiple files. Here are the details of what I'm tring to accomplish:
1. The input string comes from a file test.txt.
2. The string needs to be inserted as the second line of destination files.
3. Destination files are all the .xml files under the same direction as the batch file.
I suppose I should use a FOR loop to go through all .xml files. Something like
for /f %%i in ('dir /b *.xml') do ()
I've read though some tutorials and posts but can't find a way to add anything to files in a loop. Using Echo or TYPE doesn't seems to work for each file in a loop. How do I modify files in a loop?
Also to insert to a certain number of line some post say the file needs to be put into a variable. But my files are pretty large, which I don't want to put into variables. Is there another way to insert into a certain line in a file?
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN ('dir /b *.xml') DO (
SET line2=Y
(
FOR /f "usebackqdelims=" %%x IN ("%%i") DO (
ECHO(%%x
IF DEFINED line2 TYPE Line2.txt
SET "line2="
)
)>"%%~ni.lmx"
)
GOTO :EOF
This should work for you - but it will delete empty lines.
#echo off
set /P string=< test.txt
for %%a in (*.xml) do (
(for /F "usebackq tokens=1* delims=:" %%b in ('findstr /N "^" "%%a"') do (
if %%b equ 2 echo %string%
set "line=%%c"
setlocal EnableDelayedExpansion
echo(!line!
endlocal
)) > "%%a.new"
)
New files have .xml.new extension; you may add a couple lines to delete original .xml files and rename .xml.new ones to .xml.

windows script for delete white space from file name

I wrote a script that clear white spaces and write it to the console, but infact do nothing to file name -
#echo off&setlocal EnableDelayedExpansion
for /f "tokens=*" %%A in (
'dir C:\Inetpub\ftproot\MG_REPORTS\MG_PRO_\Network\Frank\ "* *"'
) do (set XX=%%~nxA)&echo ren "%%A" "!XX: =!"
regards,
shamie
Your for loop only sets XX to the last file name encountered. Also it probably loops over all files in the given directory and all file names containing spaces in the current working directory.
I'd do it the following way:
setlocal enabledelayedexpansion
for %%f in (C:\Inetpub\ftproot\MG_REPORTS\MG_PRO_\Network\Frank\*) do (
set "FN=%%~nxf"
set "FN=!FN: =!"
ren "%%f" "!FN!"
)

Resources