Move files to folders based on file name - windows

I am new to batch files although have searched thoroughly and found topics with a similar BUT not covering what I need.
I work with lots of documents (.pdf, .doc, .xls ) saved in C:\Tempfolder. once I am done editing I save the file name with 9 digit numbers e.g( 305123123.pdf or 306123123.pdf or .doc )
I am looking to create a batch file which will automate move the files that start with
305 to C:\Users\Omer\Documents\aaCompany or if
306 to C:\Users\Omer\Documents\bbCompany
I can have upwards of 200 files in the folder at any one time when I decide to process.
I am also curious if the batch file can monitor C:\Tempfolder and move the files 305 or 306 without executing it
Help on this is greatly appreciated.
I hope I have provided sufficient information to see if this is feasible.

You can make something like this :
#Echo off &cls
::The Input Folder
set $Dossier="C:\Tempfolder"
::The Output Folders
set $Out305="C:\Users\Omer\Documents\aaCompany"
set $Out306="C:\Users\Omer\Documents\bbCompany"
::The extensions to wait
set "$Format=*.pdf,*.xls,*.doc"
setlocal enabledelayedexpansion
:Boucle
cls&echo Waiting for file ...
for /f %%a in ('dir /b/a-d %$Dossier%\%$Format% 2^>nul') do (
set "$Fichier=%%a"
echo Treating -^> %%a
if "!$Fichier:~0,3!"=="305" move "%%~nxa" %$Out305%
if "!$Fichier:~0,3!"=="306" move "%%~nxa" %$Out306%
)
::Waiting ~5 secondes
ping localhost -n 6 >nul
::Return to the loop
goto:Boucle

Related

How could i copy multiple files 1 at a time from 2 folders to another folder with a windows batch file or vbs script

I am working with a video camera and we have a program that displays the saved videos from an SD card when inserted into a PC. At some point the manufacturer of the camera changed the directory structure and naming convention for the saved files. I would like to create a batch file or VBS Script that will reorganize the files into the old structure. This will be a quick and dirty fix for windows based PC's and until we can re-write the software which will include support for MAC's. It can be a batch file or a VBS Script but must run under a Windows command prompt with no additional software installed. The camera has front and rear cameras so there are 2 files to deal with and there could be 1 or more video captures to relocate.
The number of folders would depend on the number of videos saved, let's say there are 4 videos saved so the original structure looked like this.
- video1
- video.TS
- video2.TS
- video2
- video.TS
- video2.TS
- video3
- video.TS
- video2.TS
- video4
- video.TS
- video2.TS
The new structure looks like this
- Normal
- F
- DATETIME-000001F.TS
- DATETIME-000002F.TS
- DATETIME-000003F.TS
- DATETIME-000004F.TS
- R
- DATETIME-000001R.TS
- DATETIME-000002R.TS
- DATETIME-000003R.TS
- DATETIME-000004R.TS
The object is to move these files into the older file structure so the software can read and display them. I already have a batch file that runs when the SD card is inserted so my assumption is that I can include some script before the normal process fires to move these files around. I am pretty rusty with scripting and need some guidance.
My current script look like this.
setlocal enableextensions enabledelayedexpansion
set count=0
for %%x in (\Normal\F\*.TS) do (
set /a count += 1
mkdir video!count!
move /Y \Normal\F\*.TS \video!count!\video.TS
move /Y \Normal\R\*.TS \video!count!\video2.TS
)
endlocal
There are always 2 videos, 1 for the front camera and 1 for the rear camera so I am only using the "F" directory to get the count.
Without the move commands it creates the directory structure just fine... If there is 1 file it only creates 1 folder, if there are 8 files it creates 8 folders. but when there are multiple files it wants to put all of the files in the first folder.
I assume I would need to nest another loop but everything I have tried has failed and this is the closest attempt.
#ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\normal\F\*-*.ts" '
) DO (
SET "video2=%%~na"
SET "video2=!video2:~0,-1!R%%~xa"
FOR /f "tokens=2delims=.-" %%i IN ("%%a") DO (
SET "video=%%i"
SET /a video=1!video:~0,-1!-1000000
MD "%destdir%\video!video!" 2>NUL
MOVE /y "%sourcedir%\normal\F\%%a" "%destdir%\video!video!\video.TS" >NUL 2>nul
MOVE /y "%sourcedir%\normal\R\!video2!" "%destdir%\video!video!\video2.TS" >NUL 2>nul
)
)
)
GOTO :EOF
You would need to change the values assigned to sourcedir and destdir to suit your circumstances. The listing uses a setting that suits my system.
I deliberately include spaces in names to ensure that the spaces are processed correctly.
First, assign to %%a each filename found in the ...\F directory.
Then construct the corresponding filename in ...\R by taking the name part of %%a, removing the last character and replacing it with R, then appending the extension from %%a.
Next, derive the sequence number from %%a by selecting the second token from %%a using - and . as delimiters. Set the number part of the destination directoryname by prefixing all bar the last character of video with 1 and subtracting 1000000 to convert it to the natural form.
Create the destination directory and move in and rename the source files
[edit in response to comments]
#ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "drives=d e f g h i j k l m n o p q r s t u v w x y z"
SET "sourcedir=normal"
:: locate drive that has directory "?:\normal\f"
FOR %%d IN (%drives%) DO IF EXIST "%%d:\%sourcedir%\f\." SET "sourcedir=%%d:\%sourcedir%"&SET "destdir=%%d:\video"&GOTO founddrive
ECHO Could not find "%sourcedir%\f" on any drive
GOTO :eof
:founddrive
SET /a destnum=1
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\f\*-*.ts" '
) DO (
MD "%destdir%!destnum!" 2>NUL
SET "filename=%%~na"
set "filename=!filename:~0,-1!R%%~xa"
MOVE /y "%sourcedir%\F\%%a" "%destdir%!destnum!\video.TS" >NUL 2>nul
MOVE /y "%sourcedir%\R\!filename!" "%destdir%!destnum!\video2.TS" >NUL 2>nul
SET /a destnum+=1
)
GOTO :EOF
OK, so this version first scans for the directoryname, then picks the files and places them, with namechanges, in directory video1... on the same drive.

Windows batch file to find duplicates size files in harddisk (no matter the name and the extension)

I'd like a batch file ( Windows CMD is the interpreter, a .bat ) to do this type of task:
1) Search through a folder and its subfolders (entire harddisk)
2) Find files with the same size (no matter filename and extension)
3) Display these files (or not)
Thank you for any kind of help! :)
EDIT:
With the following commands, I can know all sizes that the files have...
#echo off
set "filename=*.*"
for %%A in (%filename%) do echo.Size of "%%A" is %%~zA bytes
but now the big problem is that you need to compare the first, with the remainder and so on!
The Batch file below should solve your problem; however, be aware that in despite of its apparent simplicity, it is based on advanced concepts, like array management in Batch files.
#echo off
setlocal EnableDelayedExpansion
rem Group all file names by size
for /R %%a in (*.*) do (
set "size[%%~Za]=!size[%%~Za]!,%%~Fa"
)
rem Show groups that have more than one element
for /F "tokens=2,3* delims=[]=," %%a in ('set size[') do (
if "%%c" neq "" echo [%%a]: %%b,%%c
)
This program may take too much time if the number of files is large or if the starting folder have a long path.

Windows Batch File that Copy Rated Files

Hi StackOverflow Community,
I would like to ask if there's a way to copy the files which are rated using a Windows Batch Job (.bat).
The files are photos, and some were rated in-camera. When all of these files (rated and unrated) are copied to my hard drive, I would like to let the batch job automatically copy out the files that are rated into another folder.
I know that my computer has no problem viewing the ratings as when I view the Properties of the rated files, a number of stars are displayed in it.
Any help is much appreciated, and thank you in advanced =)
Check tooltipinfo.bat.If the passed item is rated it will have in the output something like :
Rating: 5 Stars
so you need the tooltipinfo.bat in your directory.Then create batch file like :
(not tested)
#echo off
set folder_with_the_items=C:\photos_and_videos
set destination_folder=C:\somewhere_else
for /f "tokens=* delims=" %%a in ('dir /a:-d /b /s "%folder_with_the_items%\*"') do (
call tooltipinfo.bat "%%~fa" | find /i "Rating:" >nul 2>nul && (
copy "%%~fa" "%destination_folder%"
)
)
As you'll need to change the folder locations at the beginning of the script.

Batch file to move many listed files to one specific folder

I have a Master folder which have multiple sub folders. All the subfolders have lot images with different extensions (jpg,tif and png). The total number of images in all the subfolder are around 90000 images.
The thing is, I need to search some 500 images in Master folder and its subfolders and move the images to specified folder.
I tried a below batch script to use the text file to search through a Master folder and all subfolders and move all the files from the list and paste them into a Specified single folder.
The text file which contains file names without extension.
But my batch script is not working. It didnt throw me any error.. but nothing happens when I run it.
set FIILELIST=C:\padhu\files.txt
set FILESPATH=C:\Padhu\MasterFolder
set DESTPATH=C:\DestinationFolder
for /f %%X in (%FIILELIST%) do call :MOVE_FILES "%%X"
goto :eof
:MOVE_FILES
for /r %FILESPATH% %%I in (%~1) do echo move /qvs "%%I" "%DESTPATH%%%~pnxI"
I am very new to batch script and in learning stage. Kindly help me in this. Im very much thankful if anyone provide the correct batch script to do this.
Can you try this?
set FIILELIST=C:\padhu\files.txt
set FILESPATH=C:\Padhu\MasterFolder
set DESTPATH=C:\DestinationFolder
for /f "delims=" %%x in (%FIILELIST%) do (forfiles /p %FILESPATH% /s /m %%x.* /c "cmd /c move /y #path %DESTPATH%\#file" 2>>failed_temp.txt)
for /f "tokens=5 " %i in (failed_temp.txt) do (echo.%~i)>>failed_list.txt
del failed_temp.txt
Cheers, G
#gbabu's answer was super helpful for me.
I needed to edit it to handle file names that were absolute (full) instead of relative. And I needed to handle that they contained spaces.
Unfortunately I couldn't figure out how to log the errors like #gbabu did.
#echo off
REM See https://stackoverflow.com/a/25325529/470749
REM See https://stackoverflow.com/a/163873/470749
REM See https://stackoverflow.com/a/155950/470749
set FILELIST="K:\F\Users\my_user\Documents\My Music\JUKEBOX\5.m3u_list.txt"
set DESTPATH=C:\temp\cdrw
for /f "usebackq tokens=*" %%x in (%FILELIST%) do (copy "%%x" %DESTPATH%)
pause
These articles helped me:
https://stackoverflow.com/a/25325529/470749
https://stackoverflow.com/a/163873/470749
https://stackoverflow.com/a/155950/470749

Windows batch file - process one file at a time

Odd question - but it's driving me a bit crazy. I have a directory where multiple files can be dumped via FTP, then I need to process them one at time. So basically in this directory I could have 1.txt, 2.txt, 3.txt etc then I need to:
copy the file to an archive (if exist copy to archive)
move the file to one specific filename one at a time - data.txt <--- this is what's getting me
run a command on a legacy backend system client using that specific filename (data.txt)
run another command on legacy client using data.txt
delete data.txt
Move on to the next file and repeat
So far I've tried several methods of do loops without any luck - they all get hung up on trying to rename multiple files into one file, and that just kills me. I'd long ago since given up on batch files but annoyingly, this application has to use windows, and Server 2003 to boot.
EDIT: Here's what I've tried-
This works to do one file at a time:
if exist c:\jail\ftp*.txt copy c:\jail\ftp*.txt w:\scans\archive*.txt
if exist c:\jail\ftp*.txt move c:\jail\ftp*.txt w:\data.txt
if exist w:\data.txt C:\temp\rmtcmdb.exe
if exist w:\data.txt del w:\data.txt
I've tried multiple for loops without success, here is the latest (NOTE - I'm just trying to get past the move stage on this one, once I'm done with that I'll add in the rest):
setlocal ENABLEDELAYEDEXPANSION
FOR /f %%a IN ("c:\jail\ftp\") DO (
CALL SET /A x = !x! +1
if !x! == 1 (
CALL copy %%a w:\scans\archive*.txt
CALL move %%a w:\data.txt
)
)
I've also tried some very basic for loops, and again - nothing is getting past the move stage.
Any suggestions?
#echo off
setlocal enableextensions
for %%a in ("c:\jail\ftp*.txt") do (
set "fileName=%%~na"
setlocal enabledelayedexpansion
copy "%%~fa" "w:\scans\!fileName:*ftp=archive!%%~xa"
endlocal
move /y "%%~fa" "w:\data.txt"
start "" /wait "c:\temp\rmtcmdb.exe"
if exist "w:\data.txt" del /s "w:\data.txt" >nul 2>nul
)

Resources