List filenames and folder paths on csv using batch file - batch-file

I have a batch file that lists the full file directories (including filename at the end) onto a csv file. I need it to produce this, but also just the filename in a separate column. I would like it so that the format is Filename in first column (including extension) and full file directory in second column. The batch file I currently have is:
dir C:\Users\Administrator\Desktop\test\Files\*.tif /b /s >>
C:\Users\Administrator\Desktop\test\Output.csv
EDIT: I forgot to mention the 'Files' folder contains many subfolders so I need it to process all files from all these subfolders.
Thanks in advance.

You just need to do this:
#echo off
setlocal
set "in=C:\Users\Administrator\Desktop\test\Files\*.tif"
set "out=C:\Users\Administrator\Desktop\test\Output.csv"
if not exist "%out%" type nul>"%out%"
for /f "delims=" %%a in ('dir /b/s %in%') do (
>>%out% echo "%%~nxa","%%a"
)

Iterate and write (help for for info on the %~ variable modifiers):
#echo off
cd C:\Users\Administrator\Desktop\test\Files
for %%f in (*.tif) do echo "%%~nxf","%%~dpf" >> Output.csv

Related

batch script move files zero bytes same name

Couldn't find a way to do this, looking to store the names of all zero byte files from a folder in a list.
if exist *.xml for %%i in (*.xml) do if %%~zi==0 ECHO "%%i" >> list.txt
this works but the next step would be to read the list.txt and move all files with the same file name even if it has a different extension, also the other file isn't zero bytes that has the same file name but different extension.
example 12345.html and 12345.xml - both files would be moved because the name is the same.
any help would be amazing!
thanks
look at the modifiers in for /?. For example %%~nA would give you the name without the extension.
for /f "delims=" %%A in (list.txt) do (
echo ---- %%nA ---- files with same basename:
dir /b "%%~dpnA.*"
)

How to get size of zip files in a folder and save the name of file and size of zip file in excel

I want to create a batch file which gets the size of zip files available in a folder and save the folder name with its appropriate file size in a .CSV file. I used the below command but I only got 1 output because when the loop ends the last result is fetched in the .CSV file
for %%I in (*.txt) do #echo %%~nI,%%I >C:\Users\anoopn\Desktop\out.csv
I want the result in a format as size in a cell and whole path in different cell
Hope to get a answer soon
(for %%I in (*.zip) do echo(%%~nI,%%~zI)>C:\Users\anoopn\Desktop\out.csv
To get only the folder's name with the calculated size of all ZIP files together use this:
#ECHO OFF &SETLOCAL
FOR /f "tokens=3" %%a IN ('dir *.zip') DO (
CALL SET "size=%%free%%"
SET "free=%%a"
)
(ECHO(%cd%,%size%)>C:\Users\anoopn\Desktop\out.csv
Since you're looking for a list of .zip files, you might not want to loop over .txt files. To append to a file, you need to use >> instead of >.
FOR %%I IN (*.zip) DO #ECHO %%~nI,%%~zI >> C:\Users\anoopn\Desktop\out.csv
Update: So the question was how to recursively look in a directory tree for .zip files and output their path and size separated by a comma. It can be done like this:
FOR /F %%I IN ('dir /s /b *.zip') DO #ECHO %%I,%%~zI >> C:\Users\anoopn\Desktop\out.csv
If I run this in my JDK 1.6 folder, the output looks like this:
C:\dev\jdk16024\src.zip,19717891
C:\dev\jdk16024\jre\lib\deploy\ffjcext.zip,16801

I need a batch file to change one line of text in one directory to the same text file in a different directory

Basically, I have a text file with one line of text SetNumber=01 in 5 folders
C:\Documents and Settings\User\Desktop\Test\test.txt
C:\Folder\Test\test.txt
C:\Test\test.txt
etc.
I need to change this SetNumber=01 to different numbers monthly, for instance SetNumber=01 to SetNumber=02, in all these folders, and would like to run a batch file that would copy and replace this line of text from
C:\Documents and Settings\User\Desktop\Test\test.txt
into
C:Folder\Test\test.txt.
etc.
Any help would be greatly appreciated!!
OK, here we go. This copies the file C:\Documents and Settings\User\Desktop\Test\test.txt to all ..\test\test.txt files on the current volume (they were erased). Remove the echo command, if the output is OK:
#echo off&setlocal enabledelayedexpansion
set "sourcefile=C:\Documents and Settings\User\Desktop\Test\test.txt"
for /f "delims=" %%i in ('dir /s /b /a-d \test.txt') do (
set "fpath=%%~fi"
if "!fpath:*test\test.txt=!"=="" if not "%sourcefile%"=="%%~fi" (
echo copy "%sourcefile%" "%%~fi"
)
)
The Batch file below change all files named test.txt in any folder in the disk by inserting this line "SetNumber=%1":
#echo off
for /R \ %%a in (test.txt) do echo SetNumber=%1> "%%a"
For example, if previous Batch file is called SetNumber.bat, you may change all the files to SetNumber=02 with this command:
setnumber=02
Antonio

Check file name before deletion (windows Batch)

Is there a way using Windows Batch to read a txt file list of file names. And if the name is present in the list to preserve that file but. If the file name is not found in the txt list that it is deleted.
I namely trying to filter out a horde of temporary files automatically generated by not cleaned out by AutoCAD.
The reason for reading a txt list would allow me to add or delete file names for processing.
Yes this is possible with Windows batch. Assuming you got a list of files FilesToKeep.txt with filenames like this:
WillBeKept.txt
WillAlsoBeKept.doc
in the same directory as the following batch script:
#echo OFF
pushd "%~dp0"
set "PathToCleanup=E:\Temp\Test"
for /F "tokens=*" %%F in ('dir /S/B/A-D %PathToCleanup%\*.*') do (
findstr /S %%~nxF FilesToKeep.txt >NUL || del /Q "%%F" )
This script loops through all files in PathToCleanup and if a filename is not present in FilesToCleanup.txt then the file will be deleted.

Assistance with coding change in Windows batch script?

I have a batch script which unzip and renames each file.
Unfortunately I now need to keep the filename of the zip file it came from.
Example Jazz1.zip now unzips and the outcoming text file becomes 1.Jazz1.zip.txt.
So I want %%F to become %%F - 4- characters.
Unfortunately I want it to be Jazz1.txt.
::Setup the stage...
SETLOCAL ENABLEDELAYEDEXPANSION
SET folder=C:\P\DataSource2_W
SET count=1
::Action
CD "%folder%"
FOR %%F IN ("*.zip") DO (
"C:\Program Files (x86)\WinZip\wzunzip" %%F
MOVE *.txt "C:\P\DataSource2_W\TextFiles\!count!%%F.txt"
SET /a count=!count!+1
)
ENDLOCAL
I do not understand what you are trying to do with the COUNT variable, nor do I understand how you are handling a ZIP file with multiple .TXT files.
But I do understand that you want the base name of each ZIP file, (name without the extension). That is easy - simply use the ~n modifier (type HELP FOR from the command prompt for more info).
So if %%F = Jazz1.zip, then %%~nF yields Jazz1

Resources