0

Needs to be a simple script anyone with XP/Vista/7 can run (no PE or Powershell).

I need to move a random amount of files (eg. 1-15), and also similarly named folders (which are in a different location), to their own folder at the same time. 30 files and 30 folders to choose from:

C:\game\store\XMLs -> C:\game\mod\0.1.2\map\data  
map01_aaa.xml  
map02_bbb.xml  
map03_ccc.xml  
...  
map60_zzz.xml

C:\game\store\models -> C:\game\mod\0.1.2\sky\stuff  
01_aaa_map  
02_bbb_map  
03_ccc_map  
...  
60_zzz_map

Hope that makes sense if not I'll go to sleep and try again tomorrow. I read about a dozen questions related to moving random files thoroughly (such as this one), few hours of google, and reading robvanderwoude.com, I'm not very experienced. If anyone has any suggestions of what to do, what to read, or can give an example I can work off of to accomplish this, I'd appreciate it.

Edit: Here is the code I have so far, updated with the answer from jimhark:

@ECHO OFF & setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
SET SrcCount=0
SET SrcMax=15
FOR %%F IN (C:\game\mod\store\XMLs\*.*) DO IF !SrcCount! LSS %SrcMax% (
SET /A SrcCount += 1
ECHO !SrcCount! COPY %F C:\game\mod\0.1.2\map\data\
COPY %%F C:\game\mod\0.1.2\map\data\
SET FNAME=%%~nF
XCOPY /s "C:\game\mod\store\models\!FNAME:~3!"  "C:\game\mod\0.1.2\sky\stuff\!FNAME:~3!\"
)

It's not randomly selecting files though, even with %RANDOM%%%15, it always moves the first 8 files for example. Also the code to move the folders doesn't work, it will only move the files.

Community
  • 1
  • 1
ronald
  • 19
  • 7
  • The original code moved a random *number* of files, but doesn't randomly pick them, always picks from the beginning. You now have is set to always copy the first 15. – jimhark Jan 24 '13 at 06:39
  • I should have specified that, couldn't think of how to word it, sorry. Maybe I'll try resubmitting a new question. – ronald Jan 24 '13 at 06:49
  • In file layout description you say the source files are `C:/game/store/XMLs` (the slashes go the other way, I'm tired of retyping them every time I copy/past them), but in your .bat file you copy from `C:\game\mod\store\XMLs\`. Which is correct? – jimhark Jan 24 '13 at 07:04
  • Fixed and accepted your answer. I'll get this working, thanks again for the help. – ronald Jan 24 '13 at 07:43
  • Yes works perfect and your answer was correct from the start. I really appreciate you taking the time to thoroughly answer the question(s) and explain stuff. Doing some more reading now to figure out how to randomly pick files. Probably end up submitting another question next week. :] – ronald Jan 24 '13 at 08:06

1 Answers1

0

It would really help if you posted the .bat code you have working. Without that all I can say, based on the code you linked to, is you probably need to add something like:

First add this near the top:

setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS

Then (and I didn't rewrite the old code, but the 2 new lines at the end should be what you need):

FOR %F IN (C:\temp\source\*.*) DO IF !SrcCount! LSS %SrcMax% (
    SET /A SrcCount += 1
    ECHO !SrcCount! COPY %F C:\temp\output
    COPY %F C:\temp\output

    rem ** Here are the new lines **

    SET FNAME=%%~nF
    XCOPY /s "C:\game\store\0.1.2\sky\stuff\!FNAME:~3!"  "C:\game\mod\0.1.2\sky\stuff\!FNAME:~3!\"
)

Update 1

rem @ECHO OFF
setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
SET SrcCount=0
SET SrcMax=15

IF NOT EXIST C:\game\mod\0.1.2\map\data md C:\game\mod\0.1.2\map\data
IF NOT EXIST C:\game\mod\0.1.2\sky\stuff md C:\game\mod\0.1.2\sky\stuff

FOR %%F IN (C:\game\mod\store\XMLs\*.*) DO IF !SrcCount! LSS %SrcMax% (
  SET /A SrcCount += 1
  ECHO !SrcCount! COPY %%F C:\game\mod\0.1.2\map\data\
  COPY %%F C:\game\mod\0.1.2\map\data\
  SET FNAME=%%~nF
  ECHO XCOPY /s "C:\game\mod\store\models\!FNAME:~3!"  "C:\game\mod\0.1.2\sky\stuff\!FNAME:~3!\"
  XCOPY /s "C:\game\mod\store\models\!FNAME:~3!"  "C:\game\mod\0.1.2\sky\stuff\!FNAME:~3!\"
)

Here's my file system before:

C:\game>dir /s /b
C:\game\mod
C:\game\mod\store
C:\game\mod\store\models
C:\game\mod\store\XMLs
C:\game\mod\store\models\01_aaa
C:\game\mod\store\models\01_aaa\test.txt
C:\game\mod\store\XMLs\Map01_aaa.xml

Here it is after:

C:\game>dir /s /b
C:\game\mod
C:\game\mod\0.1.2
C:\game\mod\store
C:\game\mod\0.1.2\map
C:\game\mod\0.1.2\sky
C:\game\mod\0.1.2\map\data
C:\game\mod\0.1.2\map\data\Map01_aaa.xml
C:\game\mod\0.1.2\sky\stuff
C:\game\mod\0.1.2\sky\stuff\01_aaa
C:\game\mod\0.1.2\sky\stuff\01_aaa\test.txt
C:\game\mod\store\models
C:\game\mod\store\XMLs
C:\game\mod\store\models\01_aaa
C:\game\mod\store\models\01_aaa\test.txt
C:\game\mod\store\XMLs\Map01_aaa.xml

And here's the output:

D:\bat>docopy.bat

D:\bat>rem @ECHO OFF
D:\bat>setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
D:\bat>SET SrcCount=0
D:\bat>SET SrcMax=15
D:\bat>IF NOT EXIST C:\game\mod\0.1.2\map\data md C:\game\mod\0.1.2\map\data
D:\bat>IF NOT EXIST C:\game\mod\0.1.2\sky\stuff md C:\game\mod\0.1.2\sky\stuff

D:\bat>FOR %F IN (C:\game\mod\store\XMLs\*.*) DO IF !SrcCount! LSS 15 (
SET /A SrcCount += 1
 ECHO !SrcCount! COPY %F C:\game\mod\0.1.2\map\data\
 COPY %F C:\game\mod\0.1.2\map\data\
 SET FNAME=%~nF
 ECHO XCOPY /s "C:\game\mod\store\models\!FNAME:~3!"  "C:\game\mod\0.1.2\sky\stuff\!FNAME:~3!\"
 XCOPY /s "C:\game\mod\store\models\!FNAME:~3!"  "C:\game\mod\0.1.2\sky\stuff\!FNAME:~3!\"
)

D:\bat>IF !SrcCount! LSS 15 (
SET /A SrcCount += 1
 ECHO !SrcCount! COPY C:\game\mod\store\XMLs\Map01_aaa.xml C:\game\mod\0.1.2\map\data\
 COPY C:\game\mod\store\XMLs\Map01_aaa.xml C:\game\mod\0.1.2\map\data\
 SET FNAME=Map01_aaa
 ECHO XCOPY /s "C:\game\mod\store\models\!FNAME:~3!"  "C:\game\mod\0.1.2\sky\stuff\!FNAME:~3!\"
 XCOPY /s "C:\game\mod\store\models\!FNAME:~3!"  "C:\game\mod\0.1.2\sky\stuff\!FNAME:~3!\"
)

1 COPY C:\game\mod\store\XMLs\Map01_aaa.xml C:\game\mod\0.1.2\map\data\
        1 file(s) copied.
XCOPY /s "C:\game\mod\store\models\01_aaa"  "C:\game\mod\0.1.2\sky\stuff\01_aaa\"
C:\game\mod\store\models\01_aaa\test.txt
1 File(s) copied

D:\bat>c:

C:\game>dir /s /b

The code is working on my machine. I don't know how I can be of more help.

Update 2

What does !FNAME:~3! mean, particularly, ~3?

It's a substring operation, it drops the first three characters.

We start out with something like Map01_aaa.xml in %%F:

SET FNAME=%%~nF

Pulls out just the file name, Map01_aaa. Then:

!FNAME:~3!

Drops the first 3 characters for 01_aaa, which you indicated was the needed directory name.

C:\>set test=abcdef
c:\>echo !test:~3!
def

set /?

May also specify substrings for an expansion.

%PATH:~10,5%

would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result.

Update 3

And my bad, the folder name is actually 01_aaa_map and so on.

Then change:

XCOPY /s "C:\game\mod\store\models\!FNAME:~3!"  "C:\game\mod\0.1.2\sky\stuff\!FNAME:~3!\"

To:

XCOPY /s "C:\game\mod\store\models\!FNAME:~3!_map"  "C:\game\mod\0.1.2\sky\stuff\!FNAME:~3!_map\"
jimhark
  • 4,938
  • 2
  • 27
  • 28
  • Thanks for taking the time to reply. I added your code to the end but it doesn't do anything. No folders are copied. I've since updated the question with what code I have. – ronald Jan 24 '13 at 06:33
  • @user2003492. Doesn't do anything? *Anything.* No message was printed? What happens if you run the echo off at the beginning? What *are* you seeing? My guess is the destination directory doesn't exist. Maybe your directory setup is otherwise not as described in the question. I'm posting an update. – jimhark Jan 24 '13 at 06:59
  • What does !FNAME:~3! mean, particularly, ~3? And my bad, the folder name is actually 01_aaa_map and so on. Relevent output: SET FNAME=map60_zzz XCOPY /s "C:\game\mod\store\models\!FNAME:~3\game\mod\0.1.2\sky\stuff\!FNAME:~3!\" ) – ronald Jan 24 '13 at 07:28