I have multiple folders that each contain a config folder. In the config folder there is a .cfg file and a backup folder. I would like to copy the .cfg file to the backup folder with a date extension.
Here is my folder structure:
\Folder1\config
folder1.cfg
\backup
\Folder2\config
folder2.cfg
\backup
\Folder3\config
folder3.cfg
\backup
Each of the above folders has a config folder. In each config folder there is a .cfg file and a backup folder. How can I step through each Folder*\config directory and copy the folder*.cfg file to the backup folder and rename it to folder3.cfg.yyyymmdd? I actually have about 40 folders that I would need to this for on a Windows server. Any help would be greatly appreciated.
REM
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
for /f "delims=." %%a in ('wmic OS Get localdatetime ^| find "."') do set "wmicdt=%%a"
SET "wmicdt=%wmicdt:~0,8%"
FOR /f "delims=" %%a IN ('dir /b /ad "%sourcedir%" ' ) DO (
IF EXIST "%sourcedir%\%%a\config\%%a.cfg" (
MD "%sourcedir%\%%a\config\backup" 2>NUL
COPY /b "%sourcedir%\%%a\config\%%a.cfg" "%sourcedir%\%%a\config\backup\%%a.cfg.%wmicdt%" >nul
)
)
GOTO :EOF
You would need to change the setting of sourcedir to suit.
The SET "wmicdt=%wmicdt:~0,8%" line selects the first 8 characters of the date/time string in wmicdt, which is yyyymmdd. If you want the time as well, simply omit this line.
Here is your answer.
#echo off
:loop
for /l %%g in (1,1,3) do (
copy "Folders\Folder%%g\config\Folder%%g.cfg" "Folders\Folder%%%g\config\Backup\"
rename "Folders\Folder%%g\config\Backup\Folder%%g.cfg" "Folder %date%.cfg"
timeout>nul 1
)
:end
cls
echo.
echo ========
echo = Done =
echo ========
echo.
pause>nul
I was trying to make a file with modification date as name but i had some problems with time (22-03-2012 12:22).
This 12 ==>:<== 22 is trouble me. I don't know how to get rid of this. So instead you have current date as a file name.
Sorry for my English.
Here is file with that batch and folders, so you can see how it works
Related
I am trying to copy the listed files in file_list.txt to destination folder from source location, source folder has subfolders. my batch should be capable to search the file in source subfolders and copy to destination folder. same for copy all files with extension .exe. what is wrong with my code. I think, I have missed to search subfolders data. don't know what is the command. please help.
#Echo Off
SETLOCAL ENABLEDELAYEDEXPANSION
#color 0a
cls
set "dest=D:\destination"
set /p source=Select source path:
for /R %%f in (%source%/*.exe) do copy %%f "%dest%"
echo Copy requested files
for /f %%f in (file_list.txt) do (
for /f "tokens=*" %%F IN ('dir /S /B /A:-D "%source%/%%f"') Do (
copy "%%F" "%dest%\%USERNAME%"
)
)
pause
ENDLOCAL
I cannot see anything obviously wrong with the code you've posted, although there are possibilities for errors. You have not provided sufficient information, about your current directory, the content of the text file, how the script is invoked or any debugging information.
The following version of your code, requires that you put your file listing text file into the variable definition on line five. I've assumed that the batch file is in the same location as the file listing, and therefore used %~dp0. If it is in the current directory instead, then replace that with %CD%\, and obviously the fully qualified absolute path if neither.
Next I use some validation to try to ensure that the source, destination, and user input exist. The input location will then become the current directory.
Your provided commands are then run, before the curent directory is returned to its original location.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Color 0A
Set "listdir=%~dp0filelist.txt"
Set "dest=D:\destination"
If Not Exist "%listdir%" GoTo :EOF
If Not Exist "%dest%\" GoTo :EOF
:Ask
ClS
Set "source="
Set /P "source=Select source path: "
If Not Defined source GoTo Ask
PushD "%source:"=%" 2> NUL || GoTo Ask
For /R %%G In (*.exe) Do Copy /Y "%%G" "%dest%"
Echo Copy requested files
For /F "UseBackQ EOL=? Delims=" %%G In ("%listdir%"
) Do For /F "Delims=" %%H In ('Dir "%%~nxG" /S /B /A:-D 2^> NUL'
) Do Copy /Y "%%H" "%dest%\%UserName%"
PopD
Pause
Feel free to try the code, and provide some proper debugging information if it still fails to work as intended.
I have a bunch of folders, each containing a number of shortcut link files to mp3 files existing in completely separate folders. eg:
/rock-mp3-shortcuts
/jazz-mp3-shortcuts
/funk-mp3-shortcuts
what command would I run (or program to use) to copy all the underlying mp3 files back into the folders of shortcuts that are pointing to them.
I basically want to get all the files in each genre folder of shortcuts to then copy into my portable mp3 player.
This should work:
#echo off
FOR /r %%i in (*.lnk) do call :COPYFILE "%%i"
GOTO:EOF
:COPYFILE
set "filename=%1"
set "filename=%filename:"=%"
set "filename=%filename:\=\\%"
for /f "tokens=1* delims==" %%I in ('wmic path win32_shortcutfile where "name='%filename%'" get target /format:list ^| find "="') do (
set tatgetFile=%%J
copy /y "%tatgetFile%"
)
You'll have to create a bat file and paste my code into it. The file must be located in the folder where all your *.lnk (shortcut) files are. As you have three of them, you will have to copy the bat to each folder and execute it once. You also can automate this and use only one bat but I guess you'll figure out yourself how to do this. It will iterate over all shortcuts and copy the target files to the current folder.
Unfortunately, handling shortcuts in cmd is a pain in the 'a'. That's why we have to use wmic and win32_shortcutfile here.
You can check shortcutJS.bat with which you can create or check info about .lnk.You will need it in the same directory with this code:
#echo off
setlocal
::set your location on the line bellow
set "mp3_dir=c:\mp3_dir"
pushd "%mp3_dir%"
for /r %%# in (*.lnk) do (
for /f "tokens=1* delims=: " %%a in ('shortcutJS.bat -examine "%%~f#"^|find /i "target"') do (
echo location of %%# : %%~fb
rem !!!! remove the echo on the line bellow if everything is ok !!!!
echo copy "%%~fb" "%%~dp#"
)
)
endlocal
There is folder structure like this:
rootfolder\subfolder1\file1.txt
rootfolder\subfolder1\fileA.txt
rootfolder\subfolderX\file2.txt
rootfolder\subfolderX\fileC.txt
Need to append the foldername to the filename and include current datetime. Then move the renamed file to the rootfolder i.e. like below:
rootfolder\subfolder1_file1_<datetime>.txt
rootfolder\subfolder1_fileA_<datetime>.txt
rootfolder\subfolderX_file2_<datetime>.txt
rootfolder\subfolderX_fileC_<datetime>.txt
Does anyone know script that can do this? Thanks a lot!
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "datetime=%date:/=-%%time::=.%"
PUSHD "%sourcedir%"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\*"'
) DO (
FOR /f "delims=" %%d IN ('dir /b/a-d ".\%%a\*.txt" 2^>nul') DO (
ECHO(move "%sourcedir%\%%a\%%d" "%sourcedir%\%%a_%%~nd_%datetime%%%~xd"
)
)
popd
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
I've chosen to simply filter for .txt files - adjust the filemask to suit.
I don't know your date format or the date format you expect. You've not said whether you want the current date/time or the file's date/time to be inserted. current assumed.
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
I'm trying to copy thousands of image files and rename them with the name of the folder they are in. The file structure is:-
C:\pictures\kitcam\1\master_01.jpg
C:\pictures\kitcam\1\master_02.jpg
C:\pictures\kitcam\2\master_01.jpg
C:\pictures\kitcam\3\master_01.jpg
C:\pictures\kitcam\3001\master_01.jpg
I would like to create a new directory C:\pictures\kitcam\all and copy and rename the files above to the following naming convention:-
c:\pictures\kitcam\all\[directoryname]_filename] (pad directory name to 4 digits so that the director name 1 becomes 0001 etc)
for example:-
C:\pictures\kitcam\all\0001_master_01.jpg
Jonathan
#ECHO OFF &SETLOCAL
SET "startfolder=C:\pictures\kitcam"
SET "targetfolder=C:\pictures\kitcam\all"
FOR /r "%startfolder%" %%a IN (*.jpg) DO (
SET "fname=%%~nxa"
SET "fpath=%%~fa"
FOR /f "delims=" %%b IN ("%%~dpa.") DO SET "nname=000%%~nxb"
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO MOVE "!fpath!" "%targetfolder%\!nname:~-4!_!fname!"
ENDLOCAL
)
Look at the output and remove the word echo before move if it looks good.
Is it posible to copy and make directories automatically from file name substrings using Robocopy?
I mean i have files like these. LAJ00306130201004626.rc the first 8 chararacters are control number (LAJ00306=control number) this would be the name of the folder and the rest are the date and time (Date=130201) (time=004626).
LAJ00306130201004626.rc
LAJ00306130202004626.rc
LAJ00306130203004626.rc
LAJ00307130201004626.rc
LAJ00307130202004626.rc
and i would like to copy and create folders from the file name like under and copy the files mentioned before in the new folders.
LAJ00306
LAJ00307
I hope to be clear if necessary ask me for more information
try this, look at the output and remove the echos before MD and ROBOCOPY, if it looks good:
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcefolder=."
SET "targetfolder=X:\data"
CD /d "%sourcefolder%"
FOR %%a IN (*.rc) DO (
SET "fname=%%a"
SET "folder=!fname:~0,8!"
SET "$!folder!=1"
)
FOR /f "delims=$=" %%a IN ('set "$"') DO (
ECHO MD "%targetfolder%\%%a" 2>nul
ECHO ROBOCOPY "%sourcefolder%" "%targetfolder%\%%a" "%%a*.rc"
)
Set sourcefolder and targetfolder for your folder tree.
Try this:
#echo off
pushd "c:\source folder"
setlocal enabledelayedexpansion
for %%a in (*.rc) do (
set "name=%%a"
robocopy "%cd%" "%%a" "D:\target directory\!name:~0,8!"
)
popd
Answers to your questions are:
pushd "drive:\path" makes the location the current working directory.
popd restores the last working directory
setlocal enabledelayedexpansion allows you to change and use variables within a loop, using the !variable! syntax.
If your 2000 files are in a single folder then it should work - but test it on some sample files first so that you can see how it will work.
#ECHO OFF
SETLOCAL
SET "sourcedir=."
SET "destdir=c:\destdir"
FOR /f "tokens=1*delims=_" %%i IN (
'dir /b /a-d "%sourcedir%\*_*."'
) DO XCOPY /b "%sourcedir%\%%i_%%j" "%destdir%\%%i\"
GOTO :EOF
This should accomplish the task described. You'd need to set up the source and destination directories to suit, of course. Add >nul to the end of the XCOPY line to suppress 'copied' messages.