2015-10-19 41 views
-1

假设我有“o1usr123”作为计算机名称。 我需要创建一个文件夹“usr-123”,如果它不存在,并且 也将它用作路径。如何使用字符串拆分字符串并重新排列它以获取Windows批处理文件中的文件夹名称?

注意:o1usr不变。只有数字发生变化。

set "String=%ComputerName%" ::String is "o1usr123" 
set "User=%String:*o1=%" 
set "Num=%User:*usr=%" 
set "FName=%User%-%Num%" 

::Determine if FName exist, if not create a folder in ServerPath. 
::Then copy files from DATALOG to DATALOG\%FName% 

set "ServerPath=\\IYA-PC\DATALOG\%FName%" ::FName should be "usr-123" 
set "ClientPath=%SystemDrive%\DATALOG" 

这是正确的吗?我可以使用上面的代码将“o1usr123”变成“usr-123”吗? 你能帮助我如何确定FName是否存在,并创建一个文件夹,如果它不存在。

+1

问题是什么? – ConnorsFan

+0

对不起@ConnorsFan,我添加了问题 – IyaSheep

回答

0

如果指示的,01usr不改变,

set "FName=usr-%computername:01usr=%" 

也就是说,usr-前缀后跟computername变量没有01usr

内容一旦有了目标文件夹名称

set "ServerPath=\\IYA-PC\DATALOG\%FName%" 
set "ClientPath=%SystemDrive%\DATALOG"  

rem Just try to create the folder (and hide errors if it exist) 
md "%serverPath%" 2>nul 

rem Try to change to target folder (ensure it exists, maybe it could not be created), 
rem copy files and once done restore previous active directory 
pushd "%ServerPath%" && (
    xcopy /y "%ClientPath%" . 
    popd 
) 

pushd之后,条件执行运算符已经使用。只有当pushd成功时,它才会在括号内执行代码。

+0

是的,那是更高效的。谢谢! – IyaSheep

相关问题