
這與這個問題。
不幸的是,PowerShell 腳本在我正在使用的系統上被停用。我甚至無法使用簡單的(Get-Content)
.
我確實弄清楚瞭如何更改特定 PS 文件中的特定字串(感謝回复)。但是,我一次只能對一個 PS 文件執行此操作,並且必須通過指定 PS 文件的名稱(其硬編碼)來編輯批次檔本身。剩下的就是讓批次檔處理同一目錄(無子目錄)中的所有 PS 檔案。
這是代碼:
REM Start of Code
REM Auto-process PS files within a directory
REM Changes how PS files look when displayed
REM This batch file searches for instances of
REM "OldStringx" within the file and replaces it
REM with "NewStringx"
REM Thicken line width from 1 to 5
Set "OldString1=1 setlinewidth"
Set "NewString1=5 setlinewidth"
REM Change Courier font to Helvetica
Set "OldString2=Courier"
Set "NewString2=Helvetica-Bold"
REM To do: This batch file should process all PS files within
REM the same directory where the batch file is located
REM (Batch file and all PS files to be edited should be
REM found on the same path).
REM Specified below is the PS file to edit. Hard-coded for now.
set file="psfile_to_edit.ps"
@echo off
cd /d .
for /F "usebackq delims=" %%F in (`dir *.ps /b`) do set outFile="%%~nF_edited%%~xF"
(
for /f "skip=2 delims=" %%a in ('find /n /v "" %file%') do (
set "ln=%%a"
Setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
if defined ln set "ln=!ln:%OldString1%=%NewString1%!"
if defined ln set "ln=!ln:%OldString2%=%NewString2%!"
echo(!ln!
endlocal
)
)>%outFile%
REM Convert edited PS files to JPG
REM This requires convert.exe to work
REM Currently commented out to debug above parts.
REM convert.exe %outFile% -autocrop %outfile:~0,-4%.jpg
REM End of Code
從根本上來說,我只想讓這段程式碼處理同一目錄中的所有 PS 檔案。請幫忙。並提前致謝!
答案1
未經測試的
@ECHO OFF &SETLOCAL
cd /d .
for %%x in (*.ps) do call:process "%%~x"
goto:eof
:process
set "outFile=%~n1_edited%~x1"
(for /f "skip=2 delims=" %%a in ('find /n /v "" "%~1"') do (
set "ln=%%a"
Setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
if defined ln (
set "ln=!ln:%OldString1%=%NewString1%!"
set "ln=!ln:%OldString2%=%NewString2%!"
)
echo(!ln!
endlocal
))>"%outFile%"
exit /b
答案2
終於,經過兩週多的時間,這段程式碼終於可以運作了!歸功於 Endoro。
REM Start of Code
REM Auto-process PS files within a directory
REM Changes how PS files look when displayed
REM This batch file searches for instances of
REM "OldStringx" within the file and replaces it
REM with "NewStringx"
REM Thicken line width from 1 to 5
Set "OldString1=1 setlinewidth"
Set "NewString1=5 setlinewidth"
REM Change Courier font to Helvetica
Set "OldString2=Courier"
Set "NewString2=Helvetica-Bold"
@ECHO OFF &SETLOCAL
cd /d .
for %%x in (*.ps) do call:process "%%~x"
goto:eof
:process
set "outFile=%~n1_edited%~x1"
(for /f "skip=2 delims=" %%a in ('find /n /v "" "%~1"') do (
set "ln=%%a"
Setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
if defined ln (
set "ln=!ln:%OldString1%=%NewString1%!"
set "ln=!ln:%OldString2%=%NewString2%!"
)
echo(!ln!
endlocal
))>"%outFile%"
exit /b
REM Convert edited PS files to JPG
REM This requires convert.exe to work
REM Currently commented out to debug above parts.
REM convert.exe %outFile% -autocrop %outfile:~0,-4%.jpg
REM End of Code
到最後一部分(轉換為圖片)。再次非常感謝@Endoro(哇哈格斯!)。
答案3
只是為了記錄,linux下的一行程式碼
找到/home/usuario/micarpeta/ -name *.txt -exec sed -i "s/OldStringx/NewStringx/g" {} \