Get file name and append to beginning of line - windows

I'm trying to get a side-by-side file path and file name in a text file so I can make inserting into a database easier. I've taken a look at other examples around SO, but I haven't been able to understand what is going on. For instance, I saw this batch file to append file names to end of lines but figured that I shouldn't ask for clarification because it's 1.5 years old.
What I have is a text file of file paths. They look like this:
\\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
What I want it to look like is this:
1AD0019.tif \\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
so that I can insert it into a database. Is there an easy way to do this on Windows via Batch files?

No batch file required. From the command line:
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do #echo %~nxF %~dpF)
But that output format is risky because file and folder names can contain spaces, so it may be difficult to determine where the file name ends and the path begins. Better to enclose the file and path within quotes.
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do echo "%~nxF" "%~dpF")
if done within a batch file, then percents must be doubled.
#echo off
>"outputFile.txt" (
for /f "usebackq eol=: delims=" %%F in ("inputFile.txt") do echo "%%~nxF" "%%~dpF"
)
You should read the built in help for the FOR command. Type help for or for /? from a command prompt to get help. That strategy works for pretty much for all commands.

In powershell, this little script should do the trick. In the first line, just specify the name of the text file that contains all the file paths.
$filelist="c:\temp\filelist.txt"
foreach($L in Get-Content $filelist) {
$i = $L.length - $L.lastindexof('\') -1
$fname=$L.substring($L.length - $i, $i)
echo ($fname + ' ' + $L)
}
If you don't have powershell installed on your machine, check out http://technet.microsoft.com/en-us/library/hh847837.aspx.

#ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%i IN (yourfile.txt) DO ECHO %%~nxi %%i
)>newfile.txt
GOTO :EOF
No big drama - all on one active line, but spaced for clarity

Related

Windows Batch file - strip leading characters

I have a batch file which copies some local files up to a google storage area using the gsutil tool. The gsutil tool produces a nice log file showing the details of the files that were uploaded and if it was OK or not.
Source,Destination,Start,End,Md5,UploadId,Source Size,Bytes Transferred,Result,Description
file://C:\TEMP\file_1.xlsx,gs://app1/backups/file_1.xlsx,2018-12-04T15:25:48.428000Z,2018-12-04T15:25:48.804000Z,CPHHZfdlt6AePAPz6JO2KQ==,,18753,18753,OK,
file://C:\TEMP\file_2.xlsx,gs://app1/backups/file_2.xlsx,2018-12-04T15:25:48.428000Z,2018-12-04T15:25:48.813000Z,aTKCOQSPVwDycM9+NGO28Q==,,18753,18753,OK,
What I would like to do is to
check the status result in column 8 (OK or FAIL)
If the status is OK then move the source file to another folder (so that it is not uploaded again).
The problem is that the source filename is appended with "file://" which I can't seem to remove, example
file://C:\TEMP\file_1.xlsx
needs to be changed into this
C:\TEMP\file_1.xlsx
I am using a for /f loop and I am not sure if the manipulation of the variables %%A is different within a for /f loop.
#echo off
rem copy the gsutil log file into a temp file and remove the header row using the 'more' command.
more +1 raw_results.log > .\upload_results.log
rem get the source file name (column 1) and the upload result (OK) from column 8
for /f "tokens=1,8 delims=," %%A in (.\upload_results.log) do (
echo The source file is %%A , the upload status was %%B
set line=%%A
set line=!line:file://:=! >> output2.txt echo !line!
echo !line!
)
The output is like this.
The source file is file://C:\TEMP\file_1.xlsx , the upload status was OK
The source file is file://C:\TEMP\file_2.xlsx , the upload status was OK
I'm expecting it to dump the altered values out into a new file but it is not producing anything at the moment.
Normally I would extract from a specific character to the end of the string with something like this but it doesn't work with my For/f loop.
%var:~7%
Any pointers or a different way of doing it greatly appreciated.
Since the part to remove seems fixed it is easier to use substrings.
Also using for /f "skip=1" evades he neccessity of the external command more +1 and another intermediate file.
#echo off & setlocal EnableDelayedExpansion
type NUL>output2.txt
for /f "skip=1 eol=| tokens=1,8 delims=," %%A in (.\upload_results.log) do (
echo The source file is %%A , the upload status was %%B
set "line=%%A"
set "line=!line:~7!"
echo(!line!>>output2.txt
echo(!line!
)
File names and paths can contain also one or more exclamation marks. The line set line=%%A is parsed by Windows command processor a second time before execution with enabled delayed expansion. See How does the Windows Command Interpreter (CMD.EXE) parse scripts? Every ! inside the string assigned to loop variable A is on this line interpreted as begin or end of a delayed expanded environment variable reference. So the string of loop variable A is assigned to environment variable line with an unwanted modification if file path/name contains one or more exclamation marks.
For that reason it is best to avoid usage of delayed expansion. The fastest solution is for this task using a second FOR to get file:// removed from string assigned to loop variable A.
#echo off
del output2.txt 2>nul
for /F "skip=1 tokens=1,8 delims=," %%A in (upload_results.log) do (
echo The source file is %%A , the upload status was %%B.
for /F "tokens=1* delims=/" %%C in ("%%~A") do echo %%D>>output2.txt
)
Even faster would be without the first echo command line inside the loop:
#echo off
(for /F "skip=1 delims=," %%A in (upload_results.log) do (
for /F "tokens=1* delims=/" %%B in ("%%~A") do echo %%C
))>output2.txt
The second solution can be written also as single command line:
#(for /F "skip=1 delims=," %%A in (upload_results.log) do #for /F "tokens=1* delims=/" %%B in ("%%~A") do #echo %%C)>output2.txt
All solutions do following:
The outer FOR processes ANSI (fixed one byte per character) or UTF-8 (one to four bytes per character) encoded text file upload_results.log line by line with skipping the first line and ignoring always empty lines and lines starting with a semicolon which do not occur here.
The line is split up on every occurrence of one or more commas into substrings (tokens) with assigning first comma delimited string to specified loop variable A. The first solution additionally assigns eighth comma delimited string to next loop variable B according to ASCII table.
The inner FOR processes the string assigned to loop variable A with using / as string delimiter to get assigned to specified loop variable file: and to next loop variable according to ASCII table the rest of the string after first sequence of forward slashes which is the full qualified file name.
The full qualified file name is output with command echo and appended either directly to file output2.txt (first solution) or first to a memory buffer which is finally at once written into file output2.txt overwriting a perhaps already existing file with that file name in current directory.
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.
del /?
echo /?
for /?
See also the Microsoft article about Using command redirection operators for an explanation of the redirections >, >> and 2>nul

bulk renaming files using batch file

I have a bunch of files (and more coming) that are downloaded from the internet (pdf printed) but they do come with a lot of names that I do not need/want; so I want to have a text file with the things that I want to remove. example
I have a text file with this two lines
Chapter 13. Text and Regular Expressions - Master-PowerShell _ With Dr. Tobias Weltner - PowerShell.com
- PowerShell Scripts, Tips, Forums, and Resources.htm
I want to keep adding lines of text to that file and then call it from another batch file to remove all the lines that are in this file, I got something working that I took from foxidrive here
I could keep adding strings to the rename, but I'm wondering if I could get all the lines to remove from a text file, and I know that there are programs out there (free ones) to do that, but I'd rather use the good old DOS batch.
Thanks
Here is my code
#echo off
if Exist %CD%/temp.txt del %CD%/temp.txt
echo #echo off >%CD%/temp.txt
echo for /f "delims=" %%%%a in ('dir /a:-d /o:n /b') do call :next "%%%%a" >>%CD%/temp.txt
echo GOTO:EOF >>%CD%/temp.txt
echo :next >>%CD%/temp.txt
echo set "newname=%%~nx1" >>%CD%/temp.txt
Echo.>>%CD%/temp.txt
for /f "delims=" %%l in (%CD%\To_remove.txt) Do (
echo set "newname=%%newname:%%l=%%">> %CD%\temp.txt)
Echo.>>%CD%/temp.txt
echo ren %%1 "%%newname%%" >>%CD%/temp.txt
Start Temp.txt
Exit
I did save it as txt and opened at the end of script to see how it looks, it's just a matter of renaming it to .cmd and works.
I had to add comments and edit my post to add it all together but works
Thanks

Batch Script to Grab File Name and Add to Beginning of Each Line

I'm trying to grab the file name from a .LOG file and append it to the beginning of each line in the file, sans extension, with a space between the file name and the line contents. There are many such files in a folder and the script should be performed for each .LOG file in the folder. An example file name looks like this:
20160201.log
I want to add the file name to each of the lines in the file which look like this:
00:00:23 Left 4EEFD9 6.59418mA 0.00003mA OK
00:00:23 Right 4EEFEF 6.85377mA 0.00001mA OK
00:00:44 Left 4EEFDC 6.32207mA 0.00004mA OK
To give a result of this:
20160201 00:00:23 Left 4EEFD9 6.59418mA 0.00003mA OK
20160201 00:00:23 Right 4EEFEF 6.85377mA 0.00001mA OK
20160201 00:00:44 Left 4EEFDC 6.32207mA 0.00004mA OK
I saw this question here but it references pulling a filename from a list of filenames and placing at the beginning of each entry respectively, so that doesn't work for me. I also looked at this question, but since I need to use the file name instead of my current system time that won't work either. I don't have any code to offer...I couldn't use anything conclusive from the questions I found and I'm totally new to batch scripts.
EDIT 1: Magoo's code
Here is the current code I'm trying to run using Magoo's answer (Note the comments are my addition for troubleshooting):
#ECHO Off
SETLOCAL
SET "sourcedir=C:\Users\ckemmann\Desktop\Current"
SET "destdir=C:\Users\ckemmann\Desktop\Current\New"
::echo %sourcedir% <I check what I've assigned to sourcedir.
::echo %destdir% <And to destdir.
::pause
FOR %%f IN ("%sourcedir%\*.log") DO >"%destdir%\%%~nf.out" (
FOR /f "usebackqdelims=" %%a IN ("%%f") DO (
ECHO %%~nf %%a
)
)
::pause <I catch the batch script telling me that the system cannot find the path specified.
GOTO :EOF
As shown in the code, I get a complaint from windows saying that it can't find the file path...even though that's the one I copied from the folder properties.
EDIT 2:
Since I didn't hear back on my edit 1 I went with Arescet's answer. I sadly don't seem to have any way around the slowness of the script. My final code is the same as what he provided in his answer.
#ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir"
FOR %%f IN ("%sourcedir%\*.log") DO >"%destdir%\%%~nf.out" (
FOR /f "usebackqdelims=" %%a IN ("%%f") DO (
ECHO %%~nf %%a
)
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
Decoding...
Take the files matching "*.log" from the specified directory, assign their names to %%f
create a new file %%~nf.out in the destination directory. Read each line from %%f to %%a and echo that line, prefixed by %%~nf and a few spaces.
%%~nf means the name part only of %%f.
The redirector > createas a new file and in the position it's been placed, accepts the output of the echo into that file instead of the normal echo-to-console.
#echo off
for /r "C:\PATH TO WHEREVER\" %%G in (*.log) do (
ren "%%~G" "reWrite.temp"
for /f "tokens=*" %%H in (reWrite.temp) do (
echo %%~nG %%H >> %%~nG.log
)
del "reWrite.temp"
)
pause
This takes all .log files in "C:\PATH TO WHEREVER\", renames them, and writes the filename and data line by line into the original name, then deletes the file when done. #Magoo's answer clearly tops mine, but a different solution should help you understand different ways to solve future desires.

File path in Windows XP NTFS Format with batch for /F command

i try to figure out if i recognize enough reading the help and support center with this question.
Some informations are not linked in a for me reasonable way.
Reading about the Type command it was possible for me to recognize that using NTFS Format with my hard disk ,i have to use double quotes for path and file names with spaces.
I will show a batch code first without quotes and then with ,because with a for /F command this case double quoted do not match either with a echo command nor a type command.
#echo off
rem #
rem #
rem #
for /F %%A in (Textdokument.txt) do set Datei=%%A
rem #
rem #
echo %Datei%
rem #
rem #
echo.
rem #
rem #
set Datei=
rem #
rem #
pause
Workes proper.
The same example with double quotes just returns a ,file not found message.
for /F %%A in ("Dokumente und Einstellungen"\Benutzername\Desktop^\"Neuer Ordner"\Textdokument.txt) do set Datei=%%A
Building an easy base for gathering more reasonable file content leads to this question regarding all information i have read. To search further i would be lucky with a answer.
Best wishes
for /f %%a in (someFileName) ... will consider someFileName as a file name to read, executing the code in the do clause for each of the lines in the file.
for /f %%a in ("some file name") .... will consider "some file name" as a inmediate string to process, executing the code in the do clause for only this string.
for /f "usebackq" %%a in ("some file name") ... will consider "some file name" as a file name to read, executing the code in the do clause for each of the lines in the file.
And, the quotes should enclose the full path, so your code should be
for /F "usebackq" %%A in (
"Dokumente und Einstellungen\Benutzername\Desktop\Neuer Ordner\Textdokument.txt"
) do set Datei=%%A
Try like this with the full path and with just a " at start and one at the end and using type:
for /F %%A in (type "c:\Dokumente und Einstellungen\Benutzername\Desktop\Neuer Ordner\Textdokument.txt") do set Datei=%%A

For loop in batch file reading a file of File Paths

I want to write a Windows batch file script that will loop through a text file of FILE PATHS, do some work using data from each file path, then ultimately delete the file.
I started by running the FORFILES command and sending its output (the #PATH parameter is the full path of any file it matches) to a text file (results.txt).
I end up with a results.txt file like this:
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir1/fileA.log"
"C:/Windows/Dir2/fileC.log"
"C:/Windows/Dir3/fileB.log"
What I want to do is:
Use a FOR loop and read each line in the results.txt file
For each line (file path), strip out the directory name that the log file is sitting in (ie: Dir1, Dir2, etc..) and create a directory with that SAME name in a different location (ie. D:/Archive/Backups/Dir1, D:/Archive/Backups/Dir2, etc..) -- assuming the directory doesn't exist.
Move the actual .log file to a zip file in that directory [I have code to do this].
Delete the .log file from its original location. [Pretty straightforward]
I'm having trouble figuring out the best way to accomplish the first 2 steps. My FOR loop seems to stop after reading the very first line:
FOR /F "tokens=1,2,3,4,5,6,7,8,9,10 delims=\" %%G in ("results.txt") DO (
...
)
You don't want to parse the path with the tokens/delims options because you don't know how many directory levels you are dealing with. You want to preserve each line in its entirety. TO parse the path you want to use the FOR variable modifiers. (type HELP FOR from the command line and look at the last section of the output)
%%~pG gives the path (without the drive or file name). If we then strip off the last \, we can go through another FOR iteration and get the name (and possible extension) of the file's directory by using %%~nxA.
The toggling of delayed expansion is just to protect against a possible ! in the path. If you know that no path contains ! then you can simply enable delayed expansion at the top of the script and be done with it.
EDIT - this code has been modified significantly since Aacini pointed out that I misread the requirements. It should satisfy the requirements now.
for /f "usebackq delims=" %%G in ("results.txt") do (
set "myPath=%~pG"
setlocal enableDelayedExpansion
for /f "eol=: delims=" %%A in ("!myPath:~0,-1!") do (
endlocal
if not exist d:\Archive\Backups\%%~nxA md d:\Archive\Backups\%%~nxA
rem ::zip %%G into zip file in the D: location
rem ::you should be able to create the zip with the move option
rem ::so you don't have to del the file
)
)
I wrote this to timestamp files before offloading to SFTP.
Hope you find it useful.
The timestamp coding may seem irrelevant to your issue, but I left it because it's a good example of dissecting the filename itself.
I suggest you put an ECHO in front of the REN command for testing. Different shells may have different results.
In the end, the delayedexpansion command wasn't necessary. It was the sub-routine that fixed my issues with variables inside the loop. That could possibly be because of my OS ver. (Win 8.1) - It wouldn't hurt to leave it.
#echo off
cls
setlocal enabledelayedexpansion
if %time:~0,2% geq 10 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
if %time:~0,2% leq 9 set TIMESTAMP=%date:~10,4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
echo TimeStamp=%TIMESTAMP%
echo.
for %%G in (*.txt) do (
set OLDNAME=%%G
call :MXYZPTLK
)
dir *.txt
goto :EOF
:MXYZPTLK
echo OldName=%OLDNAME%
ren %OLDNAME% %OLDNAME:~0,-4%_%TIMESTAMP%%OLDNAME:~-4,4%
echo.
:END
You have two minor problems:
The path separator in the file is '/' but you use '\' in the for loop.
The quotes around "results.txt" stop it working.
This works. Don't write quotes to results.txt and you won't get a quote at the end of the filename.
#echo off
FOR /F "tokens=3,4 delims=/" %%I in (results.txt) DO (
REM Directory
echo %%I
REM File
echo %%J
)

Resources