简体   繁体   中英

Convert Powershell command lines into .bat file

I need to rename various files in different folders. There is a prefix 'f_' in all the filenames that need to be removed.
I managed to do this using the following lines in Powershell.

cd "C:\Users\pieter\Desktop\Folder1\Folder2\20140402_Export"
Get -ChildItem -filter "*f_*" -recurse | Rename-item -newname { $_.name -replace "f_","" }

This works, but how do I make a .bat out of this? I would like to doubleclick a script file that automatically does the same job.

Thanks!

创建一个批处理文件,然后在其中执行以下操作,它将在Powershell线程中执行您的命令并退出。

@powershell "cd 'C:\Users\pieter\Desktop\Folder1\Folder2\20140402_Export'; Get-ChildItem -filter "*f_" -recurse | Rename-item -newname { $_.name -replace 'f_','' }"

try this:

@cd /d "C:\Users\pieter\Desktop\Folder1\Folder2\20140402_Export"
for /f "delims=" %%a in ('dir /b /a-d /s "*f_*"') do (
   set "name=%%~na"
   setlocal enabledelayedexpansion
      set "new=!name:f_=!"
      ren "%%a" "!new!%%~xa"
   endlocal
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM