Hello, can you write me a batch file script for Windows that will search all…
- User
Hello, can you write me a batch file script for Windows that will search all subdirectories (and their subdirectories) for files including "NCOP" or "NCED" in the filename, and move those specific files to a new subdirectory within the same directory the files were located, the new subdirectory being called "Extra". I just want to place the batch file in the parent directory and double click to run it, I don't want to edit the batch file. I don't want any files to be overwritten, and most importantly, if the files are already inside an "Extra" folder, I don't want them to be moved at all to avoid creating duplicate Extra folders. If there are directires which have "NCOP" or "NCED" in the directory name, those should be ignored and not treated as files with those in the file name. Also, if the files including "NCOP" or "NCED" in the filename are the only files in their directory, that directory should instead be renamed to "Extra" instead of a new directory being made. Thanks!
- Assistant
Certainly! Here's a batch script that should meet your requirements:
@echo off setlocal enabledelayedexpansion rem Set the directory path to search set "directory=%~dp0" rem Loop through each directory and subdirectory for /r "%directory%" %%F in (*) do ( rem Check if the file name contains "NCOP" or "NCED" echo "%%~nxF" | findstr /i /r /c:"NCOP" /c:"NCED" >nul && ( rem Get the directory path of the file set "filePath=%%~dpF" rem Check if the file is already in an "Extra" folder echo "!filePath!" | findstr /i /r /c:"\\Extra\\$" >nul || ( rem Check if the file is the only file in the directory set "isOnlyFile=true" for /f %%D in ('dir /b "!filePath!" ^| find /v /c "::"') do ( if %%D GTR 1 set "isOnlyFile=false" ) rem Check if the directory should be renamed to "Extra" if !isOnlyFile! ( ren "!filePath!" "Extra" ) else ( rem Create the "Extra" subdirectory if it doesn't exist if not exist "!filePath!\Extra" ( mkdir "!filePath!\Extra" ) rem Move the file to the "Extra" subdirectory move /y "%%F" "!filePath!\Extra\" ) ) ) ) echo All files have been processed. pauseTo use this script, save it with a
.batfile extension and place it in the parent directory you want to search. Then, double-click on the batch file to run it. The script will search for files containing "NCOP" or "NCED" in the filename and move them to a new subdirectory called "Extra" within the same directory. It will not overwrite any files, and it will skip files that are already inside an "Extra" folder to avoid duplicates. If a directory contains only one of these files, it will be renamed to "Extra" instead of creating a new subdirectory.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.