基于files-to-change.txt批量切换配置文件.disabled后缀的脚本求助
Got it, let's figure out how to solve this exactly as you need it. You want to toggle the .disabled extension only for the specific config files listed in files-to-change.txt—no extra files touched, just the ones you specified. Here are two solid scripts to get this done, depending on your Windows environment:
PowerShell Script (Recommended for Reliability)
This script handles spaces in paths, checks for existing files, and lets you preview changes before committing:
# Read each line from the target text file Get-Content "files-to-change.txt" | ForEach-Object { # Clean up any accidental whitespace (like extra newlines or spaces) in the path $filePath = $_.Trim() # Skip empty lines if the text file has any if (-not $filePath) { return } # Only proceed if the file actually exists if (Test-Path $filePath) { # Toggle the .disabled suffix if ($filePath -like "*.disabled") { $newPath = $filePath -replace "\.disabled$", "" } else { $newPath = "$filePath.disabled" } # Preview the rename first (remove -WhatIf to apply changes) Rename-Item -Path $filePath -Destination $newPath -WhatIf } else { Write-Warning "Skipping missing file: $filePath" } }
Key Notes:
- Run this in the same folder as
files-to-change.txt(or use absolute paths for the text file and config files if needed) - The
-WhatIfflag shows you what renames would happen without actually changing files—always test this first! - It automatically skips empty lines in the text file and warns you about missing files
Batch Script (For Older Windows Environments)
If you don't have access to PowerShell, this batch script works similarly:
@echo off setlocal enabledelayedexpansion :: Loop through each line in the text file, preserving spaces in paths for /f "usebackq tokens=* delims=" %%a in ("files-to-change.txt") do ( set "filePath=%%a" :: Skip empty lines if "!filePath!"=="" goto :nextline :: Check if the file exists if exist "!filePath!" ( :: Check if the file ends with .disabled (last 9 characters) if "!filePath:~-9!"==".disabled" ( set "newPath=!filePath:.disabled=!" ) else ( set "newPath=!filePath!.disabled" ) :: Preview the rename command first (remove 'echo' to apply changes) echo ren "!filePath!" "!newPath!" ) else ( echo WARNING: File not found: !filePath! ) :nextline ) endlocal pause
Key Notes:
- The
echo renline previews changes—removeechoto actually rename files enabledelayedexpansionensures spaces in paths are handled correctly- It will pause at the end so you can see any warnings or preview output
General Tips:
- Double-check that the paths in
files-to-change.txtmatch the relative paths from where you run the script - Both scripts only modify the exact files listed in your text file—no accidental bulk changes to other config files
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

