Need help renaming files + move afterwards

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OM3
Posts: 32
Joined: 17 Mar 2014 08:43

Need help renaming files + move afterwards

#1 Post by OM3 » 13 Jan 2015 18:09

I need to rename some files in a directory (add a date at the end) a specific file and then move afterwards

In a previous thread, I got some amazing help and now have the code to rename a single file that I give as a parameter
The code I have is:

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "stamp=%DD%%MM%%YYYY%.%HH%%Min%"

:loop
ren "%~1" "%~n1.%stamp%%~x1"
shift
if not "%~1"=="" goto :loop


I know the basic loop structure I need:

Code: Select all

for /f "delims=" %%a in (' dir *.jpg /b ')  do (

ren "%%a" "%_LAST_SEGMENT_% !i!.jpg"
set /a i=i+1
)


The above is code that I have that helps me rename files so that they have the same name as the parent directory

I'm not sure how I would get the equivalents of %~1 and %~x1 ? (Since I'm not passing anything as a paramter)
Thanks to help from a previous post, I understand that these are special values relating to the first parameter

+ At the end, I need to move the renamed files to another directory

Any help would be great

Thanks


OM

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Need help renaming files + move afterwards

#2 Post by Squashman » 13 Jan 2015 18:53

Are you only showing us part of your code? I dont see where you are getting the last segment variable.

You would probably want to rename the file as you are moving it.

Regardless of that I am not fully understanding your intended goal. If you could be more specific and show examples it would probably help.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help renaming files + move afterwards

#3 Post by foxidrive » 14 Jan 2015 02:59

Squashman wrote:You would probably want to rename the file as you are moving it.


I don't quite understand the task either - but this is can move the files to another location and include the parent folder name in the filename, as Squashman suggests.

Code: Select all

@echo off
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"

set "stamp=%DD%%MM%%YYYY%.%HH%%Min%"

for %%a in ("%cd%") do set "parent-folder-name=%%~nxa"

set "target=d:\new folder"

md "%target%" 2>nul

:loop
move  "%~1" "%target%\%parent-folder-name%-%~n1.%stamp%%~x1"
shift
if not "%~1"=="" goto :loop

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Need help renaming files + move afterwards

#4 Post by Compo » 14 Jan 2015 11:22

I'm guessing that the previous respondents have been confused by the code you provided at the beginning of your opening post which I'm assuming has no relevance to your 'real' question at the bottom of that post.

If I am correct, then you could probably do what you want by dragging and dropping the folder containing your .jpg's onto this script

Code: Select all

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
If "%~1" Equ "" Echo( No input folder provided & GoTo :EndIt
If "%CD%" NEq "%~1" PushD %1
Set "d=C:\Other Directory"
For %%A In ("%CD%") Do Set "s=%%~nxA"
Set i=1
For %%A In (*.jpg) Do Call Move "%%A" "%d%\%s% %%i%%%%~xA" && Set/A i+=1
:EndIt
Ping -n 6 127.0.0.1 1>Nul & Exit/B
Please remember before running the script to first ensure that the path to your 'other directory' is correct on line 4.

Post Reply