create batch file to copy folder contents with dynamic name - batch-file

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.

Related

Select and move pdf invoices from one folder to another?

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

Batch Script Deleting Files Instead of Moving

I am still a beginner to batch scripting so I would appreciate more explanation than usual to help me if at all possible.
Purpose:
I wrote this script to try to move files based on whether they have a folder's name within their name. For example, I have a folder named "cow" and a file named "cow_5234.txt", the file would be moved into the folder. The purpose of this is to move files with company names to a company folder from a messy folder full of files so I don't think a file going to the wrong folder will be much of a problem.
Problem:
The problem I am having is that many of the files that are supposed to be transferred to folders are being misplaced. I have around 3000 files in a folder along with 300 folders and this script. After I run it, only around 100 files get placed into the correct folders, while 900 are left behind (because they don't have a folder name within them) while the rest just disappear.
What I have tried to do:
I have gone through and checked the original list of folder names, which is all correct. Then I modified the script to output the filename to a .txt file instead of move it. Instead of getting a reasonable amount of files, there are 4000 files (I checked the line count). Some of these files don't have a folder name in them. I then took a subset of around 200 files and 20 folders and ran the script with them. For some reason, only 80 files remained, 1 was placed into a folder, although it didn't have a single similar character as the folder, and the rest just disappeared. I'm not sure what is happening at all. Here is the code:
#echo off
dir /a:d /b > list.txt
for /F %%i in (list.txt) do call :process %%i
goto :eof
:process
set "search=%1"
for /f "eol=: delims=" %%F in ('dir /A:-D /b^|find "%search%"') do move /Y "%%F" "%search%"
goto :eof
Edit 1:
As recommended in the comments, I have added a "goto :eof" to the end of the block of code, however, the problem still remains.
Edit 2:
I didn't add a "goto :eof" to the end of the main function. Thanks for clarifying!

Batch file to remove copied shortcuts from public desktop

I currently use this code to copy shortcuts from a folder on a server to C:\Users\Desktop:
if not exist "%1" md "%1"
copy /y "%~dp0PlaceShortcutsHere\*.*" "%1"
This copies any shortcuts I place in the folder to the desktop.
I now need a way to remove these, baring in mind that the shortcuts in the source folder can and will change over time.
Is there a way to compare the shortcuts in the desktop and on the server and only delete the ones that are present in both folders, and only from the desktop of the computer?
These shortcuts are not all of the shortcuts on the desktop of the machines, there are others as well, hence wanting to only delete the ones present in both locations. I will also need this to be adaptive as the shortcuts present on the server will be added to or removed as needed.
This is to be deployed out through SCCM 2007/12 but I want to test it locally first.
And yes, using a GP would be easier but the GP we use has stopped working so I need a backup way of deploying shortcuts.
Took some time but I've found an answer myself. Posting this if it is useful to anyone in the future.
dir "%~dp0PlaceShortcutsHere\*" /a /b /-p /o:gen>>"%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Pushd \\<Server>\<Share>
for /F "delims=" %%G in (ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt) DO Del "C:\Users\Public\Desktop\%%G"
del "%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Breaking it down:
dir "%~dp0PlaceShortcutsHere\*" /a /b /-p /o:gen>>"%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
The above created a text file with the names of the shortcuts in it for referencing by the next line.
Pushd \\<Server>\<Share>
for /F "delims=" %%G in (ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt) DO Del "C:\Users\Public\Desktop\%%G"
The above maps the server/share that the files are in temporarily, then uses the "For /F" loop to get the file names and then delete them.
del "%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Finally, this line deleted the text file that was created.
This is useful for when the contents of the folder is always changing, it will create an up to date list of the files and then delete the list afterwards, preventing confusion.
%date:~-4,4%%date:~-7,2%%date:~-10,2%
Lastly, this little line will input the current date into the filename of the text documet, so should the final delete line not work you can see the date on it. At a guess I would also think that putting it in paths will make the script only choose the files from the current day, not any others, but I have not tested this.
Please correct me if I'm wrong on anything.

Windows batch way to replace all files in subdirectories with singular file (copy, rename all files)

I have a good command over cmd commands, but this may require a variable or a loop which is where I fail in batch commands. Please help if you can!
-- Have about 100 subdirectories each has 1-20 HTML files in it. There are about 100 HTML files in the root directory too.
-- Need to replace all HTML files in the above directories with the same HTML source (copy over existing file and keep the name the same). Basically trying to replace all existing files with a redirect script to a new server for direct bookmarked people. We are running a plain webserver without access to server-side redirects so trying to do this just by renaming the files (locked down corp environment).
Seems pretty simple. I can't get it to work with wildcards by copying the same file over to replace. I only get the first file replaced, but the rest of the files will fail. Any one with any advice?
This should do it from the command prompt. Replace % with %% for use in a batch file.
for /r "c:\base\folder" %a in (*.html) do copy /y "d:\redirect.html" "%a"
Without knowing more precisely how you want to update the file content I suggest the following rough approach.
To re-create your example, I had to create some folders. Run this command to do that:
for /l %i in (1,1,20) do mkdir fold%i
I then used this script to create some example files:
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do call :makefiles %%i
goto :EOF
:makefiles
set /a number+=1
touch %1\file%number%.txt
echo %number% >%1\file%number%.txt
I then used this script to append the text changed to the file. Not sure if that is what you wanted - probably you need something more sophisticated.
#echo off
set number=0
for /d %%i in (c:\Logs\htmltest\*) do #for %%f in ("%%i\*.txt") do call :changetext %%f
goto :EOF
:changetext
echo changing file contents to ^"changed^" for file: %1
echo changed>>%1

Replace file in "changing" directory

I know the title doesn't make sense so I'll try to elaborate:
I have a program called My App.exe and when I run it, it creates a file in a folder in AppData. The name of the folder it creates varies for every machine. Example:
When My App.exe is run on one computer, it creates this file:
%LocalAppData%\Kazankoph\My_App.exe_Url_3p43oix65iqigwb4lndfi4m34sf1xjg3\1.2.0.0\Settings.dat
When its run on another machine it creates this file:
%LocalAppData%\Kazankoph\My_App.exe_Url_6f47ntd26lwubpr3hunvt2m67sf1xtq2\1.2.0.0\Settings.dat
The only things that stay the same are the parent folder "Kazankoph", the child folder "1.2.0.0" and the file name settings.dat
It seems that the text that comes after "My_App.exe_Url_" is always 32 characters long, and is randomly generated
So my Dilemma:
I need to create a command (using cmd, batch, or SciTE script) that will take the updated settings.dat file and overwrite the old one.
I used XCOPY with the Y switch to overwrite without prompt:
XCOPY settings.dat "%LocalAppData%\Kazankoph\My_App.exe_Url_6f47ntd26lwubpr3hunvt2m67sf1xtq2\1.2.0.0" /y
But the problem with that code is that it only works for that one computer.
I want a code that will work on any computer regardless of the 32 character code.
something like this:
XCOPY settings.dat "%LocalAppData%\Kazankoph\My_App.exe_Url_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\1.2.0.0" /y
Of course this one doesn't work but you get the idea.
Anyone have any suggestions?
Run this and it should give you the folder, and copy the file from the current directory into the folder.
#echo off
for /f "delims=" %%a in (' dir /b /s /a-d "%LocalAppData%\Kazankoph\Settings.dat" ') do set "folder=%%~dpa"
echo copying settings.dat to "%folder%"
copy /y "settings.dat" "%folder%\"
pause

Resources