Glitch while trying to copy files to subdirs and rename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
machone
Posts: 31
Joined: 01 Oct 2012 18:36

Glitch while trying to copy files to subdirs and rename

#1 Post by machone » 03 Oct 2012 14:11

I'm working on a set of directories, all with varying names. Each directory contains a file with a constant string in its name which I'm using to filter on. Wherever those files with constant strings in their names exist, I need to create a subdirectory called "Deprecated" and copy the file into that new subdirectory, at the same time renaming the new file by prefixing the string "DEP " to its name...

For example, starting with:
toplevel\some random folder name\CONSTANT_STRING rest of filename.ext

and ending with
toplevel\some random folder name\CONSTANT_STRING rest of filename.ext
toplevel\some random folder name\DEPRECATED\DEP CONSTANT_STRING rest of filename.ext

I've been trying the code below:

Code: Select all

for /d %a in (".\*") do @copy "%~Fa\CONSTANT_STRING*.ext" "%~Fa\Deprecated\DEP *" 2>NUL

It works except for one glitch. The new file has part of its name overwritten instead of prefixed.

Instead of the new file being named:
DEP CONSTANT_STRING rest of filename.ext

It's named:
DEP TANT_STRING rest of filename.ext

So the 4 characters that should be prefixed, "DEP ", are actually overwriting the first 4 characters of the filename instead.. Anyone have a suggestion? I can't figure this one out.

aGerman
Expert
Posts: 4744
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Glitch while trying to copy files to subdirs and rename

#2 Post by aGerman » 03 Oct 2012 17:30

Unfortunately that seems to be the normal behavior with wildcards in the destination name. See:
viewtopic.php?f=3&t=3439

Try to use a nested loop. Untested:

Code: Select all

for /d %a in (*) do @for %b in ("%a\CONSTANT_STRING*.ext") do @copy "%b" "%a\Deprecated\DEP %~NXb"


Regards
aGerman

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Glitch while trying to copy files to subdirs and rename

#3 Post by abc0502 » 03 Oct 2012 17:39

As aGerman said, and Foxidrive mentioned another thing with a look like problem here
related to the "For /d" command and long folder name bug

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: Glitch while trying to copy files to subdirs and rename

#4 Post by machone » 03 Oct 2012 17:49

aGerman wrote:Try to use a nested loop. Untested:

Code: Select all

for /d %a in (*) do @for %b in ("%a\CONSTANT_STRING*.ext") do @copy "%b" "%a\Deprecated\DEP %~NXb"



This method names the file properly (doesn't overwrite the name) but only creates the file if the \Deprecated\ folder already exists. If not, it doesn't create it and doesn't copy the file.

aGerman
Expert
Posts: 4744
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Glitch while trying to copy files to subdirs and rename

#5 Post by aGerman » 03 Oct 2012 17:54

Use MD.

Code: Select all

for /d %a in (*) do (@md "%a\Deprecated" 2>nul &for %b in ("%a\CONSTANT_STRING*.ext") do @copy "%b" "%a\Deprecated\DEP %~NXb")

Regards
aGerman

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: Glitch while trying to copy files to subdirs and rename

#6 Post by machone » 03 Oct 2012 18:06

Nice, thanks! That seems to work so far.. now for some more testing.

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

Re: Glitch while trying to copy files to subdirs and rename

#7 Post by foxidrive » 03 Oct 2012 21:46

This is another method - the one above will create the Deprecated folder in each folder in the tree.

Code: Select all

@echo off
for /f "delims=" %%a in ( ' dir "CONSTANT_STRING*.ext" /b /s /a-d ' ) do (
echo processing "%%a"
md "%%~dpa\Deprecated" 2>nul
copy /b /y "%%a" "%%~dpa\Deprecated\DEP %%~nxa" >nul
)

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: Glitch while trying to copy files to subdirs and rename

#8 Post by machone » 04 Oct 2012 04:10

Thanks foxidrive. I didn't even realize that it would create the Deprecated folder in every folder, since every folder in my particular scenario actually has the 'CONSTANT_STRING' file in it, so in my situation it wouldn't have mattered. But having the more refined example you provided is definitely helpful.

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: Glitch while trying to copy files to subdirs and rename

#9 Post by machone » 05 Oct 2012 14:08

How would you do this process in reverse for a group of folders? For example, a folder structure like so:

\toplevel\random folder name\backup\CONSTANT_STRING rest of filename.ext

What I need to do is take the "CONSTANT_STRING rest of filename.ext" file and copy it up one level (i.e. from \backup\ to \random folder name\ )
At the same time, I need to remove "CONSTANT_STRING " from its name.
If the file already exists in the destination \random folder name\ then the file should be overwritten, or ask with a yes/no/all would be even better.

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

Re: Glitch while trying to copy files to subdirs and rename

#10 Post by foxidrive » 05 Oct 2012 22:55

This is untested:

Try it on a few folders copied from you task to a temp location.

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ( ' dir "CONSTANT_STRING*.ext" /b /s /a-d ' ) do (
echo processing "%%a"
set "name=%%~nxa"
pushd "%%~dpa"
copy /b /y "%%a" "..\!name:~15!"
popd
)

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: Glitch while trying to copy files to subdirs and rename

#11 Post by machone » 07 Oct 2012 08:50

Thanks foxidrive, will do some testing on this later today.. I presume the ~15 is the number of characters to strip from the name when the file is copied, right?

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

Re: Glitch while trying to copy files to subdirs and rename

#12 Post by foxidrive » 07 Oct 2012 08:58

Yes, stripped from the start of the name.

In other uses it can strip other characters.

Post Reply