0

我只是要完成我的脚本,为文本文件中的每一行复制文件夹结构。我们想要一个包含每个客户的子文件夹的文件夹。客户“Test”文件夹应放在\path\e$\Relaties\T\Test 中。因此,对于每个 beginletter,一个特定的文件夹,对于其余的数字或字符,一个“_Overig”(其他)文件夹。

这一切都很好,但我有特殊字符的问题,如 %* / \ ' " 等等。在下面的示例中,我想删除文件夹名称中的所有 ',因为 Windows 不支持。我这样做set var=!var2: =! 但它不起作用。此外,当文本文件中的一行包含 ! 时,脚本也会崩溃。我可以通过将 ^! instedad of ! 放入批处理文件中来逃避它,但是因为它是由我们的 Intranet 生成的,我希望下面的批处理文件只是为了处理 ! 并自行转义它。

请帮帮我。

setlocal EnableDelayedExpansion
set file=E:\list.txt

FOR /F "delims=~" %%i IN (!file!) DO (

echo %%i

set var2=%%i
set var=!var2:*=!

set str=!var:~0,1!
IF /i !str!==A ( set letter=1&&set str=A)
IF /i !str!==B ( set letter=1&&set str=B)
IF /i !str!==C ( set letter=1&&set str=C)
IF /i !str!==D ( set letter=1&&set str=D)
IF /i !str!==E ( set letter=1&&set str=E)
IF /i !str!==F ( set letter=1&&set str=F)
IF /i !str!==G ( set letter=1&&set str=G)
IF /i !str!==H ( set letter=1&&set str=H)
IF /i !str!==I ( set letter=1&&set str=I)
IF /i !str!==J ( set letter=1&&set str=J)
IF /i !str!==K ( set letter=1&&set str=K)
IF /i !str!==L ( set letter=1&&set str=L)
IF /i !str!==M ( set letter=1&&set str=M)
IF /i !str!==N ( set letter=1&&set str=N)
IF /i !str!==O ( set letter=1&&set str=O)
IF /i !str!==P ( set letter=1&&set str=P)
IF /i !str!==Q ( set letter=1&&set str=Q)
IF /i !str!==R ( set letter=1&&set str=R)
IF /i !str!==S ( set letter=1&&set str=S)
IF /i !str!==T ( set letter=1&&set str=T)
IF /i !str!==U ( set letter=1&&set str=U)
IF /i !str!==V ( set letter=1&&set str=V)
IF /i !str!==W ( set letter=1&&set str=W)
IF /i !str!==X ( set letter=1&&set str=X)
IF /i !str!==Y ( set letter=1&&set str=Y)
IF /i !str!==Z ( set letter=1&&set str=Z)

IF !letter!==1 ( set pad=\\fs1\e$\Data\Relaties\!str!\!var!) ELSE (set pad=\\fs1\e$\Data\Relaties\_Overig\!var!)
set oudpad=\\fs1\e$\Data\Klanten\!var!
xcopy "\\fs1\e$\Data\Relaties\_template\_template" "!pad!" /T /E /O /I

robocopy "!oudpad!\adviesrapporten" "!pad!\Documentatie\Adviesrapporten" /ZB /COPY:DAT /R:3 /W:1

set letter=0

)

编辑:

使用 set var=!var:**=! 修复了星号问题

但是,如果我想要一个带有 ! 在其中,脚本会忽略它。

4

1 回答 1

1

Ok - try this:

@ECHO OFF
SETLOCAL
FOR /f "delims=" %%i IN (E:\list.txt) DO (
 ECHO "%%i"|FINDSTR /i /b /r .[a-z] >nul
IF ERRORLEVEL 1 (set letter=_Overig) ELSE (SET letter=%%i&CALL SET letter=%%letter:~0,1%%)
SET var=%%~i
CALL :transmute
)
GOTO :eof

:transmute
SETLOCAL enabledelayedexpansion
SET var=!var:^|=!
SET var=!var:^>=!
SET var=!var:^<=!
SET var=!var:%%=!
SET var=!var:^&=!
endlocal&SET var=%var%
SET var=%var:?=%
SET var=%var::=%
SET var=%var:[=%
SET var=%var:]=%
SET var=%var:`=%
SET var=%var:'=%
SET var=%var:{=%
SET var=%var:}=%
SET var=%var:/=%
SET var=%var:\=%
SET nvar=%var:"=%
SET "var="
:tmlp
IF NOT "%nvar:~0,1%"=="*" IF NOT "%nvar:~0,1%"=="=" SET var=%var%%nvar:~0,1%
SET nvar=%nvar:~1%
IF DEFINED nvar GOTO tmlp
SET pad=\\fs1\e$\Data\Relaties\%letter%\%var%
set oudpad=\\fs1\e$\Data\Klanten\%var%
ECHO XCOPY "\\fs1\e$\Data\Relaties\_template\_template" "%pad%" /T /E /O /I
ECHO robocopy "%oudpad%\adviesrapporten" "%pad%\Documentatie\Adviesrapporten" /ZB /COPY:DAT /R:3 /W:1

GOTO :eof

which at least doesn't crash on data file

name 1
allo! allo!
"good grief!"
@last!
%percents-problem or not?%
how about ^carets?
NOT carrots
(what does this do?)
)and what about this?
;semi to start?
-dash
^caret?
^^double-caret?
&ampersand schmooze
!now this is a test!!
=can be skipped
:colon, of course
    and ever-loving spaces
*s fell on stockton
9" nails
|pipe?
?question mark??
>greater?
<less??
.dot
,comma
12345
`that's all
o'connor
]close-square
[open-square
{and brace-open
}or close
/slash
\slosh

Essentially, after checking that the line starts with a letter, each name is massaged to remove awkward characters and eventually the name and directory are displayed.

Neither the XCOPY nor the ROBOCOPY is activated; you'd need to remove the preceding ECHO after checking...

于 2013-04-17T18:50:22.757 回答