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.*"
)
Related
I am trying to find all the MP3 files in a given directory, then export it to a file, before trying to use the contents of the file as an input.
All the instances of file copy are working fine, except for the file locations which contain spaces. How should I address this in my current code. Please refer to the screenshot below
Contents of my MP3_Location.txt file are:
C:\Test\asdad.MP3
C:\Test\New folder\werwer.MP3
C:\Test\OneDrive - Backup\asdasdasdad.MP3
REM Exporting the location of the MP3 file in a given directory
DIR /s/b "C:\Test\*.MP3" >> C:\Software\MP3_Location.txt
REM Trying to copy the files based on the previous Output
FOR /F %%G IN (C:\Software\MP3_Location.txt) DO c:\windows\system32\xcopy
"%%G" C:\Software\MP3\ /Y
Edit 1: Trying to use Delims now, as suggested (perhaps not using it correctly)
REM Exporting the location of the MP3 file in a given directory
DIR /s/b "C:\Test\*.MP3" >> C:\Software\MP3_Location.txt
REM Trying to copy the files
FOR /F %%G "tokens=* delims=" IN (C:\Software\MP3_Location.txt) DO c:\windows\system32\xcopy "%%G" C:\Software\MP3\ /Y
Used the command
FOR /F "delims=" %%G IN
I have a folder in a directory which has hundreds of zipped folders in it and also many files inside it.for example ,
Folder
cs2-20150613-6014-0000-201071.zip
cs2-20150613-abc.bin
cs2-20150613-xyz.bin
cs2-20150614-6014-0000-201066.zip
cs2-20150614-abc.bin
cs2-20150614-xyz.bin
I want to attach the last 6 digits of the folder name to all the files after extracting,
cs2-20150613-abc.bin-201071
cs2-20150613-xyz.bin-201071
Can anyone suggest me how it can be done in batch file
#echo off
for /F "tokens=1,2,5 delims=-." %%a in ('dir /B *.zip') do (
ren %%a-%%b-*.bin %%a-%%b-*.bin.%%c
)
This code assume that the format shown is fixed: cs2-YYYYMMDD-xxxx-xxxx-######.zip. If this format may change, an adjustment in the code is needed.
Here's a short script I wrote that should do what you need
#echo off & setlocal enabledelayedexpansion
for %%a in (*.zip) do (
7z x -o"%temp%\%%~na" "%%a" > Nul <&2
set "suffix=%%~na"
set "suffix=!suffix:~-6!"
for %%b in (%temp%\%%~na\*) do move /Y "%%b" "%cd%\%%~nxb-!suffix!" > Nul <&2
rd /s /q "%temp%\%%~na"
)
It requires that you have 7z on your path for extracting the compressed files for .zip folders.
The suffix added it the extracted files is always the 6 characters before the extension for the .zip file, ignoring weather or not they're digits.
This code utilizes the idiom > Nul <&2 to redirect all output (stdout and stderr) to the Nul file, effectively silencing the program.
Can anyone assist me in writing a batch script that searches for file names and outputs the location of the files to a text file. Example I have a file called list.txt located in a folder, C:\LocateFiles\list.txt. Located in the list.txt file are about 25 file names that I wish to determine if they are anywhere on the C:\ drive. If it locates any of the file names identified in the file list.txt it will output the path of all files found to a single file in C:\LocatedFiles\results.txt.
A million thanks,
Johnny Mac
#ECHO OFF
FOR /F %%F IN (C:\LocateFiles\List.txt) DO DIR /s/p/b %%F.* >> C:\LocateFiles\finds.txt
Save that as LocateFiles.cmd and place it in whichever directory you wish to search, note that C:\ is very large and will take quite a while! as in, forever, seriously, i really wouldnt, your call...
the file finds.txt will have the entire path for any file that matches up to the file names listed in List.txt
Also note, this finds files of any extension, but the filename itself must match exactly to whats in List.txt
The solution below search the files in the current directory just once, so it run faster.
#echo off
dir /S /B /A-D | findstr /I /G:C:\LocateFiles\list.txt > C:\LocatedFiles\results.txt
EDIT: New method added
The method below may run even faster. It is necessary to complete a timing test.
#echo off
setlocal EnableDelayedExpansion
rem Read file names from file list and assemble a long string with this format:
rem "filename1.ext*" "filename2.ext*" ...
set "fileList="
for /F "delims=" %%a in (C:\LocateFiles\list.txt) do set fileList=!fileList! "%%a*"
rem Search the files from current directory downwards
(for /R %%a in (%fileList%) do echo %%a) > C:\LocatedFiles\results.txt
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
I am trying to find a way to create a Windows batch script that will look at a target folder full of .pdf files and move (or copy) them to another directory with existing subfolders based on the filename.
The files and folders are names of actual people. I want to be able to get that person's pdf into their existing folder using a script.
Say I have 2 files in my folder; smithJohn015.pdf and thomasBill030.pdf.
I would like to be able to put smithJohn015.pdf into folder SmithJohn and thomasBill030.pdf into folder ThomasBill.
I don't want the script to create new folders or overwrite existing files if there's a duplicate filename.
I'm not necessarily looking for anyone to write a script for me, but if anyone can just get me started in the right direction it would be appreciated.
Try modifying this answer for your evil purposes.
#echo off
setlocal
pushd "c:\path\to\PDFs"
for /d %%I in (c:\Path\To\People\*) do (
for %%F in (*) do (
for /f %%A in ('echo %%~nF ^| find /i "%%~nI"') do (
set /p a="Moving %%F to %%I... "<NUL
move "%%F" "%%I" >NUL
echo Done.
)
)
)
popd
You'll need to add a check for if not exist pdffile before the move, but there's a starting direction for you anyway.
The following assumes the target subfolders' location contains only the subfolders where the PDFs may go and that every PDF that you want to move has a name formatted as the corresponding subfolder's name followed by exactly three characters (same as in your examples):
#ECHO OFF
FOR /D %%D IN ("D:\path\to\subfolders\*") DO (
MOVE "D:\path\to\PDFs\%%~nD???.pdf" "%%D"
)
Or as a one-liner to execute directly at the command prompt:
FOR /D %D IN ("D:\path\to\subfolders\*") DO (MOVE "D:\path\to\PDFs\%~nD???.pdf" "%D")
folderlist.txt contains all names of folders in which you want to copy respective PDFs.
xcopy is copy command. format xcopy "source" "destination" /parameters.
pause is just to keep command window on
for /F "tokens=*" %%A in (folderlist.txt) do (
xcopy "E:\path\to\source folder\<prefix>%%A<suffix>.pdf" "E:\path\to\destination folder\<prefix>%%A<suffix>\" /s)
pause
You can use wildcards in source & destination paths.