热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何移动具有相似名称的随机数量的文件和文件夹?-Howtomoverandomamountoffiles&folderswithsimilarnames?

NeedstobeasimplescriptanyonewithXPVista7canrun(noPEorPowershell).需要是一个简单的脚本,任何人都可以运

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

需要是一个简单的脚本,任何人都可以运行XP / Vista / 7(没有PE或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:

我需要将随机数量的文件(例如1-15)以及类似命名的文件夹(位于不同位置)同时移动到它们自己的文件夹中。 30个文件和30个文件夹可供选择:

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.

希望有意义,如果不是我会去睡觉,明天再试一次。我读了十几个与彻底移动随机文件有关的问题(比如这个),几个小时的google,以及阅读robvanderwoude.com,我不是很有经验。如果有人有任何关于做什么,阅读什么的建议,或者可以给出一个例子我可以用来完成这个,我会很感激。

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

编辑:这是我到目前为止的代码,更新了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.

它不是随机选择文件,即使使用%RANDOM %%% 15,它总是会移动前8个文件。移动文件夹的代码也不起作用,它只会移动文件。

1 个解决方案

#1


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:

如果您发布了有效的.bat代码,那将非常有用。没有这一点我可以说,根据您链接的代码,您可能需要添加如下内容:

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):

然后(我没有重写旧代码,但最后的2个新行应该是你需要的):

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?

什么!FNAME:~3!意思是,特别是〜3?

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

这是一个子串操作,它会删除前三个字符。

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

我们从%% F中的Map01_aaa.xml开始:

SET FNAME=%%~nF

Pulls out just the file name, Map01_aaa. Then:

只拉出文件名Map01_aaa。然后:

!FNAME:~3!

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

删除01_aaa的前3个字符,表示您需要的目录名称。

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.

而我的不好,文件夹名称实际上是01_aaa_map,依此类推。

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\"

推荐阅读
author-avatar
Jenny821010
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有