Moving file in Batch - batch-file

I have a Batch script where I want to move the files from one folder to another. But the challenge here is the name of the folder where I am moving the file to changes based on date also file name changes based on current date e.g. date_file_name. I tried this approach,
#ECHO OFF
for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
set dow=%%i
set month=%%j
set day=%%k
set year=%%l
)
set day=%month%%day%%year%
echo %day%
set f_name=BulkScript.sh
echo %f_name%
echo "Renaming the downloaded script...."
ren %f_name% %day%_%f_name%
echo "Move the file"
move C:/Users/Downloads/%day%_%f_name% C:/Users/Downloads/%day%/
So I wanna move file from Downloads to day folder. The % here gives me error, of course I am new to Batch programming. Any help will be appreciated.

Related

How to batch transfer rename?

There are two folders holding equal amounts of files between them.
I'd like to apply the names from one set ; to the other set of files in no particular order. Inherit the name, but retain the extension .
Input files are .bmp Output files are .ini ( has a gear symbol ).
Example :
folder 1- Flowers.bmp ; folder 2- blank.ini
. To this:
folder 1- Flowers.bmp ; folder 2- Flowers.ini
There would be more files , but equal .
The .ini files are all copies . So they may have a generic name and numbered if that matters to know . Those would all receive one name each from the other .bmp files in the other folder.
Normally I have both folders situated on the Desktop .
I make sure both folders have equal number of files between them . That would be a constant .
I'm trying to stream line some menial repetitive daily tasks .
I did search and what I have found does not really help.
#ECHO OFF
SET "vers=%~1"
IF "%vers%" == "" SET /P "vers=Enter Vers: "
FOR %%F IN (file_XX_*.*) DO CALL :process "%%F"
GOTO :EOF
:process
SET "name=%~nx1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "name=!name:_XX_=_%vers%_!"
RENAME %1 "%name%"
ENDLOCAL
Hoping to find a solution to this finally .
Ok, here is a slightly long version, but it makes sure it gets the content of each folder.
Note You must ensure that the path to both folders are correctly specified in the third and fourth line:
#echo off
setlocal enabledelayedexpansion
set "source=%userprofile%\Desktop\folder 1"
set "destination=%userprofile%\Desktop\folder 2"
set /a num=0
for %%a in ("%source%\*") do (
set /a num+=1
set "fr!num!m=%%~na"
)
set /a oldn=!num!
set /a num=0
for %%b in ("%destination%\*") do (
set /a num+=1
set "to!num!r!=%%~nxb"
set "ext=%%~xb"
)
pushd "%destination%"
for /l %%i in (1,1,!oldn!) do ren "!to%%ir!" "!fr%%im!%ext%"
popd
pause
You can remove pause at the bottom of the script, once you are happy with the output.
This is not the most effective way, but considering your limited batch experience, I guess an understandable approach is better than an optimized one (using array-like variables).
#echo off
setlocal enabledelayedexpansion
(for %%A in ("folder 1\*.bmp") do echo %%~nA)> "%temp%\names.txt"
<"%temp%\names.txt" (
for %%A in ("folder 2\*.ini") do (
set /p "name="
ECHO ren "%%A" "!name!.ini"
))
The first for loop creates a list of the desired names (from Folder 1). The modifier %%~nA returns the name only.
The second for processes each file in Folder 2 and takes a name from the previously generated file for each file. This depends on the fact that the number of files in both folders are the same, else you may get undesired results.
I disabled the actual ren command by just echoing it. Check the output and only when you are satisfied, remove the ECHO.

Batch copy multiple files from different folders with their paths listed in a txt file, and rename any duplicates

I've been trying to tackle this problem for few days now to no avail. I have no programming experience whatsoever and this task has been driving me nuts.
I have a txt file that has a list of paths to files that need to be copied. Some 8000 paths are in this file.
Copying each item isn't such a big deal as I can just add the copy command and destination before/after each path.
The crux of my issue is that many of these files have the same filename and when they're in different directories it's not a problem.
However I need all the files in the same destination folder and it keeps overwriting itself.
To sum up, I have a .txt file that basically looks like this:
D:\Big folder\Folder\Subfolder a\filea.file
D:\Big folder\Folder3\Subfolder za\filek.file
D:\Big folder\Folder\Subfolder ds\filed.file
D:\Big folder8\Folder\Subfolder p\filea.file...
I need some tool that will let me copy all of these files into one destination folder, and make sure any duplicates get renamed so that they aren't overwritten.
such that filea.file and filea.file become filea.file and filea1.file
EDIT: so far I've come up with
FOR /F "tokens=* usebackq" %i IN (`type "C:\Users\username\Desktop\completelist.txt"`) DO COPY "%i" "E:\destination\"
which does the read and copy job but not the rename part
Save script below to Copy.bat, open Cmd Prompt from the script directory, and run the bat. It works well for me. Post exact errors, if any.
#echo off
setlocal enabledelayedexpansion
set file=%userprofile%\Desktop\completelist.txt
set "dest=E:\destination" & set "i=" & pushd !dest!
for /f "usebackq tokens=*" %%G in ("%file%") do (
call :rename %%~nG %%~xG %%G
copy "%%G" "%dest%\!target!" >nul )
popd
exit /b
:rename
set "target=%1!i!%2"
:loop
set /a i+=1
if exist "!target!" set "target=%1!i!%2" & goto :loop
set "i=" & echo Copied %3 to !target!
exit /b

Batch file that will modify date in txt file

I have a line in a text file that I'm trying to modify using a batch file. The line in this case is the lastexportdate=2014-01-01. I'm trying to get the batch file to modify this from
lastexportdate=2014-01-01
to
lastexportdate= Current timestamp.
Is this possible to have this equal the timestamp of my computer? Not certain how to go about tackling this, so if anyone could help that would be awesome. I'm using Windows 7.
File contents below:
root.footer=</CREDITEXPORT>
root.header=<CREDITEXPORT>
emailaddronsuccess=test#test.com
emailaddronerror=test#test.com
rerunexportdate=2014-09-06
errorfilelocation=C:\\
lastexportdate=2014-01-01
xmloutputlocation=C:\\
#echo off
setlocal
rem getting current time stamp
set "beginJS=mshta "javascript:var t=new Date();var dd=t.getDate();var mm=t.getMonth()+1;var yyyy=t.getFullYear();if(dd^<10) dd='0'+dd;if(mm^<10) mm='0'+mm;close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write("
set "endJS=));""
:: FOR /F does not need pipe
for /f %%N in (
'%beginJS% yyyy+'-'+mm+'-'+dd %endJS%'
) do set cds=%%N
echo cds=%cds%
rem processing the file
rem set the location of the file
set "content_file=c:\some.file"
break>temp.file
for /f "usebackq tokens=1* delims==" %%a in ("%content_file%") do (
if "%%a" NEQ "lastexportdate" echo %%a=%%b>>temp.file
if "%%a" EQU "lastexportdate" echo %%a=%cds%>>temp.file
)
Not tested.Just change the location of the content_file and check if the created temp.file is ok.
To replace the file add move temp.file "%content_file%" at the end.

How to use batch job to add the file "create date" into all the files in a directory?

I have a folder that gets a new file added everyday to the folder with the same file name but incremental extension such as .001, .002, .003, etc. However, if there's no file within the folder it starts at .001 again.
The problem is they are all named the same and if I move them to another folder to archive them it would just overwrite the same file over and over again. I could create a folder each day with the date with only one file in it, but that seems a bit redundant.
Is there a way to look at the create date of each file and rename it to the create date?
I've gotten this far, but it looks like for this situation I have to use a static file name, how to loop through the entire directory?
SET filename = C:\test.001
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
rename c:\test.001 C:\test_%filedatetime%.txt
move C:\*.txt C:\archive\
this provides the correct sort order:
#echo off &setlocal disableDelayedExpansion
set "startfolder=%userprofile%\test"
cd /d "%startfolder%"
for %%a in (*) do (
for /f "delims=." %%b in ('wmic datafile where "name='%startfolder:\=\\%\\%%~a'" get lastmodified^|find "."') do (
echo(ren "%startfolder%\%%~a" "%%~b.txt"
)
)
Remove echo to get it working.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetdir=c:\sourcedir"
SET "destdir=c:\destdir"
PUSHD "%targetdir%"
FOR %%a IN (*.*) DO (
SET "timestamp=%%~ta"
SET "timestamp=!timestamp:/=_!
SET "timestamp=!timestamp::=_!
SET "timestamp=!timestamp:.=_!
SET "timestamp=!timestamp:,=_!
SET "timestamp=!timestamp: =_!
ECHO MOVE "%%a" "%destdir%\%%~na.!timestamp!"
)
GOTO :EOF
This should work with any file in the nominated target directory where the name does not include ! or ^.
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)
The gymnastics around timestamp are intende to replace /: with _ since these are illegal filename characters. Space., are similarly replaced - they're legal but often painful.
If you want the destination filename to be name.003.timestamp, remove the ~na from the destination name.
Try like this :
SET $path=The_path_who_contain_the_FILES
FOR /F "DELIMS=" %%f IN ('dir "%$path%" /a-d/b') DO (
SET filedatetime=%%~tf
move "%%~dpnxf" "C:\archive\test_%filedatetime%.txt")

batch file to name folder based on filename

I have managed to copy and manipulate a batch script that does the following:
looks at the last 4 characters of a .csv file
creates a folder with this name
moves the file into the folder
#echo off & setlocal EnableDelayedExpansion
echo.
pushd "%~dp0"
for %%j in (*.csv) do (
set file=%%~nj
set folder=!file:~-4!
if !folder!==FI_2 set folder=!file:~-6!
if not exist !folder! md !folder!
move "%%~j" "!folder!"
echo "%%~j" -^> "!folder!"
)
popd
echo.& echo.Done
:: End_Of_Batch
The problem I have is that I need to group the files by month. The file names look like this:
BS_IDX_LEVEL_YYYYMMDD_BAFI.csv
(obviously the YYYYMMDD will be replaced with the date of the file)
So any file with the name BS_IDX_LEVEL_20111231_BAFI.csv would go into a folder named "1112" (date formate of folder is YYMM)
A file with the name BS_IDX_LEVEL_20111115_BAFI.csv would go into a folder names "1111"
Is there someway I can alter this script so that before organising the files by their filenames it groups them by date first?
If I can explain anything further please let me know
While you are looping through and getting the last 4 characters of the file name, you can also look at the dates. I didn't know if the start of the file will be fixed or subject to change, but I thought it reasonable to assume the date_end syntax would be consistent.
set filedate=!file:~-9,-7!
This will extract the characters between 9th and 7th from the end of the String stored in !file!. With the syntax you have, the will be the two digit month number. This will also avoid any confusion with 2 or 4 digit dates.
Once you have both this and the other folder name, you can simply create both folders at once and place the file directly into the subfolder, so you will still only need to make one pass over the fileset.
#echo off & setlocal EnableDelayedExpansion
echo.
pushd "%~dp0"
for %%j in (*.csv) do (
set file=%%~nj
set folder=!file:~-4!
if !folder!==FI_2 set folder=!file:~-6!
set filedate=!file:~-9,-7!
SET datefolder=!folder!\!filedate!
if not exist !folder! md !folder!
if not exist !folder!\!filedate! md !folder!\!filedate!
move "%%~j" "!datefolder!"
echo "%%~j" -^> "!datefolder!"
)
popd
echo.& echo.Done
:: End_Of_Batch

Resources