0

I am just about completing my script on copying a folder structure for every line in a textfile. We want a folder with subfolders for every customer. The customer "Test" folder should be placed in \path\e$\Relaties\T\Test. So for every beginletter a specific folder and for the rest like numbers or characters an "_Overig" (other) folder.

This all works fine, but I have problems with special characters like %* / \ ' " and so on. In the example beneath, I want to delete all 's in the foldername, since Windows does not support that. I do this with set var=!var2:=! but it does not work. Also, when a line in the textfile contains an !, the script also crashes. I can escape it by putting ^! instedad of ! in the batch file, but since it is generated by our intranet I want the batch file below just to deal with !'s and escape it by itself.

Please help me out.

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

)

Edit:

Fixed the asterisk thing by using set var=!var:**=!

But still if I want a foldername with an ! in it, the script ignores it.

4

1 に答える 1

1

わかりました-これを試してください:

@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

少なくともデータファイルでクラッシュしない

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

基本的に、行が文字で始まることを確認した後、各名前をマッサージして厄介な文字を削除し、最終的に名前とディレクトリが表示されます。

XCOPYももROBOCOPYアクティブ化されていません。ECHOチェック後に前のものを削除する必要があります...

于 2013-04-17T18:50:22.757 に答える