• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

script help needed

Homerboy

Lifer
I have many user directories:

/123
/456
/789
...

all located within /USERS/
each one of these directories has 12 files that are the same names

ABC.txt
DEF.txt
GHI.txt
...

yet each of those files within their respective parent directory are UNIQUE to their parent directory. (same names different content).

I need those 12 files moved to a new directory within the parent directory.

So I'd end up with:

ABC.txt
DEF.txt
GHI.txt
...

all now located within:
/123/new directory
/456/new directory
/789/new directory
...

Does that make any sense at all? Its difficult to explain.
I would like a script to do this automatically (I can edit the specific names of the 12 files)
Hopefully something like this can be accomplished.


 
You can do that in one line with the find command:

User@L4 ~/test
$ find
.
./123
./123/ABC.txt
./123/DEF.txt
./123/GHI.txt
./456
./456/ABC.txt
./456/DEF.txt
./456/GHI.txt
./789
./789/ABC.txt
./789/DEF.txt
./789/GHI.txt

User@L4 ~/test
$ find -maxdepth 1 -type d -not -name . -exec mkdir {}/new \; -exec mv {}/ABC.txt {}/DEF.txt {}/GHI.txt {}/new \;

User@L4 ~/test
$ find
.
./123
./123/new
./123/new/ABC.txt
./123/new/DEF.txt
./123/new/GHI.txt
./456
./456/new
./456/new/ABC.txt
./456/new/DEF.txt
./456/new/GHI.txt
./789
./789/new
./789/new/ABC.txt
./789/new/DEF.txt
./789/new/GHI.txt

User@L4 ~/test
$
 
Originally posted by: ppdes
You can do that in one line with the find command:

User@L4 ~/test
$ find
.
./123
./123/ABC.txt
./123/DEF.txt
./123/GHI.txt
./456
./456/ABC.txt
./456/DEF.txt
./456/GHI.txt
./789
./789/ABC.txt
./789/DEF.txt
./789/GHI.txt

User@L4 ~/test
$ find -maxdepth 1 -type d -not -name . -exec mkdir {}/new \; -exec mv {}/ABC.txt {}/DEF.txt {}/GHI.txt {}/new \;

User@L4 ~/test
$ find
.
./123
./123/new
./123/new/ABC.txt
./123/new/DEF.txt
./123/new/GHI.txt
./456
./456/new
./456/new/ABC.txt
./456/new/DEF.txt
./456/new/GHI.txt
./789
./789/new
./789/new/ABC.txt
./789/new/DEF.txt
./789/new/GHI.txt

User@L4 ~/test
$

nice, elegant solution.
 
yeap very nice... let me take it one step "deeper" though
since I have 95+ directories in /USESRS/ I'd rather not have to string it all out line by line in the .bat repeating

/123
./123/ABC.txt
./123/DEF.txt
./123/GHI.txt

for 95 directories and 12 files would be kinda tedious. Anyway to take it up 1 directory depth to /users/ and say anything 1 directory into /users/ copy these 2 files (I'd obviously have to have 12 lines, one for each specific file) Does that make sense?
 
Originally posted by: Homerboy
yeap very nice... let me take it one step "deeper" though
since I have 95+ directories in /USESRS/ I'd rather not have to string it all out line by line in the .bat repeating

/123
./123/ABC.txt
./123/DEF.txt
./123/GHI.txt

for 95 directories and 12 files would be kinda tedious. Anyway to take it up 1 directory depth to /users/ and say anything 1 directory into /users/ copy these 2 files (I'd obviously have to have 12 lines, one for each specific file) Does that make sense?
That's exactly what it already does. The lines you quote were output from the "find" command with no arguments that I ran to show you the directory structure before running the actual command. It is not something you would type. The only things I typed in that were the lines proceeded by the $ prompt.

The only thing you would type is this part (with /users/ as the current directory):
find -maxdepth 1 -type d -not -name . -exec mkdir {}/new \; -exec mv {}/ABC.txt {}/DEF.txt {}/GHI.txt {}/new \;

"find" by itself finds everything in or under the current directory, including things inside other directories.

Adding:
-maxdepth 1 -type d -not -name .
restricts it to only directories immediately under the current directory.

Adding:
-exec mkdir {}/new \;
tells it to make a new directory in everything that it finds instead of printing it.

Adding:
-exec mv {}/ABC.txt {}/DEF.txt {}/GHI.txt {}/new \;
tells it to move those 3 files in each thing that it finds to the new directory it made in each thing it finds.

To move additional files you'd simply add "{}/JKL.txt" etc. in front of the "{}/new" argument.
 
telling me "FIND: parameter incorrect"
find -maxdepth 1 -type d -not -name . -exec mkdir {}/CLS \; -exec mv {}/TEST.txt {}/TEST2.txt {}/TEST3.txt {}/CLS \;
 
Here's the version of find I tested on (running in Cygwin on Vista Business):
User@L4 ~/test
$ find --version
GNU find version 4.3.8
Built using GNU gnulib version 2007-05-26
Features enabled: O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0)

What OS and version of find are you using? You used *nix paths in your first post and didn't mention an OS, so I assumed you weren't Windows. Windows also has a find command, but it is completely different.
 
Oh no... sorry its entirely a Windows environment. This would specifically be run on a 2k3 server... I was wondering when did the "find" command function like that in Win32.
So... back to square one.

Add to OP: *for Win32
 
If they were just windows users on a machine...
xcopy "c:\documents and settings\%userid%\ABC.txt" "c:\documents and settings\%userid%\NEW DIR\ABC.txt"
xcopy "c:\documents and settings\%userid%\DEF.txt" "c:\documents and settings\%userid%\NEW DIR\DEF.txt"
xcopy "c:\documents and settings\%userid%\GHI.txt" "c:\documents and settings\%userid%\NEW DIR\GHI.txt"
...

But I see your problem... I'll try and think of another way.
 
Originally posted by: acole1
If they were just windows users on a machine...
xcopy "c:\documents and settings\%userid%\ABC.txt" "c:\documents and settings\%userid%\NEW DIR\ABC.txt"
xcopy "c:\documents and settings\%userid%\DEF.txt" "c:\documents and settings\%userid%\NEW DIR\DEF.txt"
xcopy "c:\documents and settings\%userid%\GHI.txt" "c:\documents and settings\%userid%\NEW DIR\GHI.txt"
...

But I see your problem... I'll try and think of another way.

right... 1 user, no problem, but 90+ equals a problem
 
This website has helpful batch file tips and info.

It's possible you can create an xcopy routine that is something like...
xcopy "c:\%1\*.*" "c:\%1\new_folder\"
xcopy "c:\%2\*.*" "c:\%2\new_folder\"
xcopy "c:\%3\*.*" "c:\%3\new_folder\"

You can create the 90+ variables in an excel sheet (this cell = prev cell+1)... and then when you call the batch file you feed it a list of your user names.

Not a real simple solution but it might work.
 
Dropping something like this in a batch file seems to work:
for /d %%D in (*) do (
mkdir %%D\CLS
move %%D\ABC.txt %%D\CLS
move %%D\DEF.TXT %%D\CLS
)


Example test run on Windows (I only typed the dir and test.bat commands):
C:\test>dir /s /b
C:\test\123
C:\test\456
C:\test\test.bat
C:\test\123\ABC.txt
C:\test\123\DEF.txt
C:\test\456\ABC.txt
C:\test\456\DEF.txt

C:\test>test.bat

C:\test>for / %D in (*) do (
mkdir %D\CLS
move %D\ABC.txt %D\CLS
move %D\DEF.TXT %D\CLS
)

C:\test>(
mkdir 123\CLS
move 123\ABC.txt 123\CLS
move 123\DEF.TXT 123\CLS
)
1 file(s) moved.
1 file(s) moved.

C:\test>(
mkdir 456\CLS
move 456\ABC.txt 456\CLS
move 456\DEF.TXT 456\CLS
)
1 file(s) moved.
1 file(s) moved.

C:\test>dir /s /b
C:\test\123
C:\test\456
C:\test\test.bat
C:\test\123\CLS
C:\test\123\CLS\ABC.txt
C:\test\123\CLS\DEF.txt
C:\test\456\CLS
C:\test\456\CLS\ABC.txt
C:\test\456\CLS\DEF.txt
 
Originally posted by: ppdes
Dropping something like this in a batch file seems to work:
for /d %%D in (*) do (
mkdir %%D\CLS
move %%D\ABC.txt %%D\CLS
move %%D\DEF.TXT %%D\CLS
)


Example test run on Windows (I only typed the dir and test.bat commands):
C:\test>dir /s /b
C:\test\123
C:\test\456
C:\test\test.bat
C:\test\123\ABC.txt
C:\test\123\DEF.txt
C:\test\456\ABC.txt
C:\test\456\DEF.txt

C:\test>test.bat

C:\test>for / %D in (*) do (
mkdir %D\CLS
move %D\ABC.txt %D\CLS
move %D\DEF.TXT %D\CLS
)

C:\test>(
mkdir 123\CLS
move 123\ABC.txt 123\CLS
move 123\DEF.TXT 123\CLS
)
1 file(s) moved.
1 file(s) moved.

C:\test>(
mkdir 456\CLS
move 456\ABC.txt 456\CLS
move 456\DEF.TXT 456\CLS
)
1 file(s) moved.
1 file(s) moved.

C:\test>dir /s /b
C:\test\123
C:\test\456
C:\test\test.bat
C:\test\123\CLS
C:\test\123\CLS\ABC.txt
C:\test\123\CLS\DEF.txt
C:\test\456\CLS
C:\test\456\CLS\ABC.txt
C:\test\456\CLS\DEF.txt

file test today and report back!
 
Back
Top