Select and move pdf invoices from one folder to another? - batch-file

I have a directory with around 82000 .pdf files, and need only around 3000 of them.
I am trying to figure out how I can select only the ones that I want, and move them to a new directory.
For example, I have:
C:\Users\jkelly\Desktop\Clients\TAX\Invoices\INV.655486.pdf
C:\Users\jkelly\Desktop\Clients\TAX\Invoices\INV.655487.pdf
C:\Users\jkelly\Desktop\Clients\TAX\Invoices\INV.655488.pdf
C:\Users\jkelly\Desktop\Clients\TAX\Invoices\INV.655489.pdf
C:\Users\jkelly\Desktop\Clients\TAX\Invoices\INV.655490.pdf
All I want to do is be able to copy and paste all of the file paths somewhere, and use a command to move them to a new directory.
Any help would be greatly appreciated.

Put the list of file directories in files.txt and save this code as a .bat file.
#echo off
cls
echo Input destination folder
echo.
set /p fol=Destination:
for /f %%a in (files.txt) do move /-Y %%a %fol%
exit

Related

Copy list of files from directory+subfolders to another folder

First of all, I am a total beginner. I was trying an ultimate script bat file solution for a game. To not annoy you with details, let me tell you what I tried to do.
Example:
I have 17 files. 10 of them are .jpg and 7 of them are .obj files.
The images are in path \mods\images and the objects are in path \mods\models.
I want to list all 17 missing files in a list.txt
The bat file will read that list, search for the files and paste them into the TTSmissing folder
and here are my problems:
The bat script only looks exactly into the source path, but not into subfolders (that's why I wrote \mods\images\, to test if it works) so
what I basically want is: \Tabletop Simulator\Mods\ as source path and
the script shall look into all subfolders, too.
The list.txt only works, when the filenames also have their extensions. is it possible to change the script so i don't need the extension? so it will only look for names and copy the files? (example: the names in the list have to be like: hello123.jpg. It's not working when its only hello123.)
How do I need to change the bat script if i don't want a list.txt but just put the list of files right into the bat file?
#echo off
mkdir %USERPROFILE%\Desktop\TTSmissing
set src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods\Images
set dst_folder=%USERPROFILE%\Desktop\TTSmissing
set file_list=%USERPROFILE%\Desktop\list.txt
for /f "tokens=*" %%i in (%file_list%) DO (
xcopy /S/E "%src_folder%\%%i" "%dst_folder%"
)
pause
#echo off
setlocal
set "src_folder=%USERPROFILE%\Documents\My Games\Tabletop Simulator\Mods"
set "dst_folder=%USERPROFILE%\Desktop\TTSmissing"
set "file_list=%USERPROFILE%\Desktop\list.txt"
set "ext_list=.gif .jpeg .jpg .mp4 .obj .pdf .png .webm"
if not exist "%dst_folder%" md "%dst_folder%"
for /d /r "%src_folder%\" %%A in (*) do (
pushd "%%~A" && (
for /f "usebackq delims=" %%B in ("%file_list%") do (
for %%C in (%ext_list%) do (
if exist "%%~B%%~C" (
echo copy /y "%%~B%%~C" "%dst_folder%\"
)
)
)
popd
)
)
You just want to copy files so copy is easier to use than xcopy. The code will echo the copy command to test whether it is working how you want it. If satisfied, remove the echo in front of copy and run the code again to do the actual copy process.
A for /d /r loop will recursively iterate the subdirectories in %src_folder%. pushd will change the current directory to each subdirectory so as can work relative to the source files.
The for /f loop will iterate each line from %file_list%. The simple for loop will iterate each of %ext_list%. If current "name.extension" exists, it will be copied to %dst_folder%.
If you set variables names in a script, it is usually a good idea to use setlocal to keep the variables defined local to the script.
To view help for a command, use command /?. This will work for many of commands used in the code.
View command /? help for copy, for, if, setlocal ...

Output list of matching files to .txt then move files, use txt to later return files to original folder

There is really 2 parts to this question:
I have a folder full of files that I need to temporarily move to a
new location.
However I will later need to move the files back to their original
location(s).
I have a single folder, full of files and folders that looks like this:
C:\VIDEO\My Video 1\My Video 1.mkv
C:\VIDEO\MyVideo2\MyVideo2.mkv
C:\VIDEO\My.Video.3\My.Video.3.mkv
I need to:
1. Recursively find all *.mkv files within C:\VIDEO folder
2. Output a list of the existing Dir structure/file and folder names/paths to a .txt file
3. Then Move all *.mkv files from C:\VIDEO to another folder C:\Temp
(I do not want to retain the original folder structure during this move)
At a later time I then need to:
4. Search for all *.mkv files in C:\Temp
5. Use the .txt file to help move each *.mkv file back into their original location
I guess this will probably require 2 separate batch files.
Here is my current progress:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
:: Setup
set "SourcePath=C:\VIDEO"
set "DestPath=C:\Temp"
:: Output Items To Txt File
for /f "delims=\" %%A in ('dir "%SourcePath%"\*.mkv') DO echo "%SourcePath%">>"%DestPath%"\output.txt
:: Move Matching Items
for /f "tokens=*" %%a IN ('dir "%SourcePath%"\*.mkv') DO move /y "%SourcePath%\%%a" "%DestPath%"
Can somebody please help?
You want to move a tree to a flat destination (knowing you later want to move it back and have to recreate the tree)? Why on earth would one do that...
But ok:
#echo off
set "SourcePath=C:\VIDEO"
set "DestPath=C:\Temp"
echo #echo off > MoveBack.bat
for /r "%SourcePath%" %%A in (*.mkv) do (
ECHO move "%%~fA" "%DestPath%\"
>> MoveBack.bat echo move "%DestPath%\%%~nxA" "%%~dpA"
)
echo done. To move them back, execute MoveBack.bat
Instead of logging the moved files to a text file and later iterate over that file, it's easier to just build a "restore" script.
For a description of the %%~ modifiers, read the output of for /?
NOTE: I disarmed the move command for security reasons. If you are sure it works as intended, remove the ECHO.
Note: it's possible to have duplicate file names in a folder tree. This script does not account for that (saying: you may lose data in this case)
The final product:
#echo on
set "SourcePath=H:\FIXED"
set "DestPath=H:\Temp"
echo #echo on > MoveBack.bat
for /r "%SourcePath%" %%A in (*.mkv) do (
move "%%~fA" "%DestPath%\"
>> MoveBack.bat echo move "%DestPath%\%%~nxA" "%%~dpA"
)
#echo done. To move them back, execute MoveBack.bat
is now working.

Batch file Drag and Drop multiple files from one folder to a new folder Error

So I have the batch file below.
#ECHO OFF
FOR %%I IN (.) DO SET FolderName=%%~nxI
MKDIR "C:\%FolderName%"
for %%i in (%*) do (
move "%%~i" "C:\%FolderName%"
)
So when I drag and drop multiple files into the batch file, it will take the name of the folder that holds the files that I drag and drop and make a new folder at C:\ with the same name and then move the files into the new folder at C:\
Example: The folder that holds the files that I want to move is name Shop. Then the folder Shop is located at
...\ground\bell\tower\Shop
Using the batch file will make a new folder name Shop at C:\
Example
C:\Shop
The batch files works when I drag and drop about 100 files at once. The problem is that when I drag and drop 300 files at once, it returns the Error that says "The filename or extension is too long". I am able to move the files manually so I know that it can not be cause by a filename being too long.
Then I made a new batch file thinking maybe it is a problem with the move command or my for loop. So I wrote the batch file below.
#echo off
FOR %%I IN (.) DO SET FolderName=%%~nxI
MKDIR "C:\%FolderName%"
MOVE "%cd%\*.*" "C:\%FolderName%"
Now the second batch file above works just about the same as the first batch file. Just that with the second batch file, I only need to drag and drop one file from the folder that I want to move and it will move all the files in the first folder to a new folder at C:\ even if the first folder had 1000 files.
My question is why does the first batch file fail if I drag and drop too many files at once. Using the second batch file work, so it can not be because of the move command or is it? Since I am moving all the files from one folder to the other, the second batch file fits my need and was wondering if there will be any problems with the second batch file or a better way of doing this.
When you Drag-n-Drop Files on you Bat File, your Bat is actually called as if you would have called it in your command prompt. So if you drag three files on your Bat i.e.
Testfile.txt
Testfile.md
Testfile.jpg
Then actually your Bat is called like this:
C:\MyBatch.BAT Testfile.txt Testfile.md Testfile.jpg
If you call more it is obviously something like:
C:\MyBatch.BAT Testfile.txt Testfile.md Testfile.jpg Testfile01.txt Testfile01.md Testfile01.jpg Testfile02.txt Testfile02.md Testfile02.jpg Testfile03.txt Testfile03.md Testfile03.jpg Testfile04.txt Testfile04.md Testfile04.jpg Testfile05.txt Testfile05.md Testfile05.jpg etc...
Ah, can you read the rest of the line? Ok, this is not as long as 1000 Files but get the point what the difference is in your scripts. Your command line buffer will not be able to capture that much input.
Actually the size of how many characters you can enter in you command prompt varies a bit from system to system, but there was something like 8k in winXP, i reckon it is still the same.
http://support.microsoft.com/kb/830473 <-- that could help more concerning max.
And since the length of your command prompt is so "short" you have to find different methods for longer file trails - as you did - you could also overcome this, if you output the files you want to copy in a text file and then use that as input for your copy bat.
dir /b >filelist.txt
so now that you have this file list, then you just read the text file line by line:
for /f "delims=" %%i in (filelist.txt) do echo D|xcopy "C:\FolderName\%%i" "c:\temp\%%i" /i /z /y
so this will actually read your file list.txt and will (for each filename in list) press D key and pipes it to the XCopy command.
So piping a filelist to your command is a much better way, where actually in your case, if you really want the whole dir and not just a selection, copying directories is faster than copying file by file.
Hope you Question is answered.
If I am not mistaken, you are trying to create a folder with the parent folder as name. So before you ask again the same question in another post, I will code what you are seeking.
#echo off
setlocal
set "destination=c:\destination"
for %%i in (%~1) do set "parent=%%~pi" &goto:next
:next
for %%i in ("%parent:~0,-1%") do set "parent=%%~nxi"
for %%i in (%*) do (
echo:
if not exist %destination%\%parent%\nul ( mkdir "%destination%\%parent%" )
move "%%~i" "%destination%%parent%"
)
In addition, a command with many arguments is not a problem in the previous code.
For example: Write a batch file with the following code
#echo off
setlocal
set "destination=c:\destination"
for %%i in (%~1) do set "parent=%%~pi" &goto:next
:next
for %%i in ("%parent:~0,-1%") do set "parent=%%~nxi"
for %%i in (%*) do (
echo:
if not exist %destination%\%parent%\nul (echo mkdir "%destination%\%parent%")
echo move "%%~i" "%destination%%parent%"
)
call it test-move.bat and put the following long command-line of 250 parameters in cmd:
test-move.bat TestFile001.txt TestFile002.txt TestFile003.txt TestFile004.txt TestFile005.txt TestFile006.txt TestFile007.txt TestFile008.txt TestFile009.txt TestFile010.txt TestFile011.txt TestFile012.txt TestFile013.txt TestFile014.txt TestFile015.txt TestFile016.txt TestFile017.txt TestFile018.txt TestFile019.txt TestFile020.txt TestFile021.txt TestFile022.txt TestFile023.txt TestFile024.txt TestFile025.txt TestFile026.txt TestFile027.txt TestFile028.txt TestFile029.txt TestFile030.txt TestFile031.txt TestFile032.txt TestFile033.txt TestFile034.txt TestFile035.txt TestFile036.txt TestFile037.txt TestFile038.txt TestFile039.txt TestFile040.txt TestFile041.txt TestFile042.txt TestFile043.txt TestFile044.txt TestFile045.txt TestFile046.txt TestFile047.txt TestFile048.txt TestFile049.txt TestFile050.txt TestFile051.txt TestFile052.txt TestFile053.txt TestFile054.txt TestFile055.txt TestFile056.txt TestFile057.txt TestFile058.txt TestFile059.txt TestFile060.txt TestFile061.txt TestFile062.txt TestFile063.txt TestFile064.txt TestFile065.txt TestFile066.txt TestFile067.txt TestFile068.txt TestFile069.txt TestFile070.txt TestFile071.txt TestFile072.txt TestFile073.txt TestFile074.txt TestFile075.txt TestFile076.txt TestFile077.txt TestFile078.txt TestFile079.txt TestFile080.txt TestFile081.txt TestFile082.txt TestFile083.txt TestFile084.txt TestFile085.txt TestFile086.txt TestFile087.txt TestFile088.txt TestFile089.txt TestFile090.txt TestFile091.txt TestFile092.txt TestFile093.txt TestFile094.txt TestFile095.txt TestFile096.txt TestFile097.txt TestFile098.txt TestFile099.txt TestFile100.txt TestFile101.txt TestFile102.txt TestFile103.txt TestFile104.txt TestFile105.txt TestFile106.txt TestFile107.txt TestFile108.txt TestFile109.txt TestFile110.txt TestFile111.txt TestFile112.txt TestFile113.txt TestFile114.txt TestFile115.txt TestFile116.txt TestFile117.txt TestFile118.txt TestFile119.txt TestFile120.txt TestFile121.txt TestFile122.txt TestFile123.txt TestFile124.txt TestFile125.txt TestFile126.txt TestFile127.txt TestFile128.txt TestFile129.txt TestFile130.txt TestFile131.txt TestFile132.txt TestFile133.txt TestFile134.txt TestFile135.txt TestFile136.txt TestFile137.txt TestFile138.txt TestFile139.txt TestFile140.txt TestFile141.txt TestFile142.txt TestFile143.txt TestFile144.txt TestFile145.txt TestFile146.txt TestFile147.txt TestFile148.txt TestFile149.txt TestFile150.txt TestFile151.txt TestFile152.txt TestFile153.txt TestFile154.txt TestFile155.txt TestFile156.txt TestFile157.txt TestFile158.txt TestFile159.txt TestFile160.txt TestFile161.txt TestFile162.txt TestFile163.txt TestFile164.txt TestFile165.txt TestFile166.txt TestFile167.txt TestFile168.txt TestFile169.txt TestFile170.txt TestFile171.txt TestFile172.txt TestFile173.txt TestFile174.txt TestFile175.txt TestFile176.txt TestFile177.txt TestFile178.txt TestFile179.txt TestFile180.txt TestFile181.txt TestFile182.txt TestFile183.txt TestFile184.txt TestFile185.txt TestFile186.txt TestFile187.txt TestFile188.txt TestFile189.txt TestFile190.txt TestFile191.txt TestFile192.txt TestFile193.txt TestFile194.txt TestFile195.txt TestFile196.txt TestFile197.txt TestFile198.txt TestFile199.txt TestFile200.txt TestFile201.txt TestFile202.txt TestFile203.txt TestFile204.txt TestFile205.txt TestFile206.txt TestFile207.txt TestFile208.txt TestFile209.txt TestFile210.txt TestFile211.txt TestFile212.txt TestFile213.txt TestFile214.txt TestFile215.txt TestFile216.txt TestFile217.txt TestFile218.txt TestFile219.txt TestFile220.txt TestFile221.txt TestFile222.txt TestFile223.txt TestFile224.txt TestFile225.txt TestFile226.txt TestFile227.txt TestFile228.txt TestFile229.txt TestFile230.txt TestFile231.txt TestFile232.txt TestFile233.txt TestFile234.txt TestFile235.txt TestFile236.txt TestFile237.txt TestFile238.txt TestFile239.txt TestFile240.txt TestFile241.txt TestFile242.txt TestFile243.txt TestFile244.txt TestFile245.txt TestFile246.txt TestFile247.txt TestFile248.txt TestFile249.txt TestFile250.txt

create batch file to copy folder contents with dynamic name

I am absolutely brand new to any kind of development but need a batch job to copy a file from one folder to another.
The problem is that the source folder is dynamically named. The folder name will contain the current date and a suffix number (eg. "TestRun_20141106_13") - so I will never be able to determine the 'latest' version of the folder before running the batch / copy job.
Can anyone help please? I know this will be easy for someone but as I said, I am a complete noob!!
Thanks in advance.
Jamie
Yes, I havent been doing .bat for that long either, but i think i can help!
Here is a code for the movement of the file!
Dealing wiwth dynamically named folder...
#echo off
set /p txtfile=Filename without Path assumes c:\:
echo.%txtfile%
copy %txtfile% z:\testing\dealer.txt
echo Come back to this window when Agent is done with process. The copy file will be deleted.
#pause
copy %txtfile% c:\somefolder\namedsuccess\%txtfile%
del z:\testing\dealer.txt
exit
You will have to place your own variables in there my friend!
For moving of the files!
Easy part!
move /-y "Folder Path that files are in*(Any specific keyword?)*" "(Dest. folder)"
#ECHO OFF
FOR /F "TOKENS=*" %%A IN ('DIR "C:\Example" /s /b /a:d') DO SET CurrentDir=%%A
#ECHO.%CurrentDir%
Replace "C:\Example" with the Path your Folders are in,
save it to a File (.bat/.cmd) and execute.
The last step - Echo will return the most bottom foldername.

Windows Batch File: Look for directory, if not exist, create, then move file to it

I am trying to create a batch file, or other script, to take the contents of one folder to a folder containing its name in another directory. For example:
ShowName.Episode.Title.mkv should be moved to \movies\showname. if \movies\showname\ doesn't exist, the script would create it.
There are, on average, 10-15 files at a time that would need moved.
Any ideas?
Thanks
You can conditionally create the folder with:
if not exist \movies\showname mkdir \movies\showname
To move a file into it:
move ShowName.Episode.Title.mkv \movies\showname
To get more information about these commands, open a command prompt and type:
help if
and
help move
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
FOR /f "tokens=1-4delims=." %%a IN (
'dir /b /a-d "%sourcedir%\*.*.*.mkv" '
) DO (
MD "%destdir%\%%a" 2>NUL
MOVE "%sourcedir%\%%a.%%b.%%c.%%d" "%destdir%\%%a\"
)
GOTO :EOF
This should do your moves. You'd have to change the directory names, of course - no idea where your source directory is, but destination becomes \movies in your case.
May be an idea to try ECHO MOVE first, just to make sure that the move is as-required.
The 2>nul on the MD suppresses the error messages saying the directory already exists.
Adding >nul to the end of the MOVE line will suppress the file moved message.

Resources