How to overwrite files with names starting from nl_ with sp_ - batch-file

I have folder A and B. Folder A has files like: a.mp3 and a.txt and folder B has: b.mp3 and b.txt. What I want to do here is copy and rename the content of the folder A to B so that the files can be overwritten.
Here is an example code on how to overwrite and keep the same file name in folder B:
XCOPY /HECY A\a.txt B\b.txt
ButI don't want to type all the file names to copy and overwrite the files in folder B.
Any help will be appreciated.

This should work. It will copy all A\nl_*.* files to B\, renaming the nl to sp and overwriting the files.
setlocal enabledelayedexpansion
for %%a in (A\nl_*.*) do (
set file=%%~nxa
set file=!file:~2!
xcopy /hecyi "%%a" "B\sp!file!"
)

Renames a file/directory or files/directories.
RENAME [drive:][path][directoryname1 | filename1] [directoryname2 | filename2]
REN [drive:][path][directoryname1 | filename1] [directoryname2 | filename2]
Example
to rename a directory "sampel" to "sample"
rename c:\sampel sample
to rename all text files to files with .bak extension.
rename *.txt *.bak
to rename all files in a particular folder with specific prefix(eg: 1_NEW)
rename * 1_NEW*
4.Rename the file "normal sample.txt" to "example sample.txt". Whenever dealing with a file or directory with a space, it must be surrounded with quotes. Otherwise you'll get the "The syntax of the command is incorrect." error.
rename "normal sample.txt" "example sample.txt"

You may do that directly with xcopy /HECYI A\nl_*.* B\sp_*.* command:
C:>dir A /b
nl_t.txt
nl_test.txt
nl_test.xyz
nl_testLarge.txt
C:>dir B /b
C:>xcopy A\nl_*.* B\sp_*.*
A\nl_t.txt
A\nl_test.txt
A\nl_test.xyz
A\nl_testLarge.txt
4 File(s) copied
C:>dir B /b
sp_t.txt
sp_test.txt
sp_test.xyz
sp_testLarge.txt

Related

Copying and renaming multiple files in a .bat file with FOR ... DO xcopy

I want to move several thousand files (.jpg and .pdf) from one location to another and then rename each file.
My 'copy_rename_docs.csv' input file contains two columns:
the first column containing the original path AND file names for each file that I want to move and rename
and the second column containing the new paths AND file names that I want to move and rename each file to
like this:
\\myapp\data\e3\9b\e39bf5e8d745833418d3edcd97c6c7589716970b,\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\data\09\f5\09f58d1a1345f67a36154e99ffedb27308fa4292,\\myapp\test_migration\09\f5\nxf7.pdf
so "e39bf5e8d745833418d3edcd97c6c7589716970b" is a SHA1 hash of "Test File-xxx - SS1.pdf"
To copy and rename the files I tried this batch file:
#echo off
FOR /F "tokens=1,2 delims=," %%a IN (copy_rename_docs.csv) DO (xcopy /e /i "%%a" "%%b\*")
pause
exit
But instead of copying and renaming the files to this:
\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\test_migration\09\f5\nxf7.pdf
It is creating a FOLDER for each file with the FILE names as the FOLDER names and copying the files with their original file names into each FOLDER, like this:
\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf\e39bf5e8d745833418d3edcd97c6c7589716970b
\\myapp\test_migration\09\f5\nxf7.pdf\09f58d1a1345f67a36154e99ffedb27308fa4292
What I want is:
\\myapp\test_migration\e3\9b\Test File-xxx - SS1.pdf
\\myapp\test_migration\09\f5\nxf7.pdf
What's wrong with my batch file?
And is xcopy the best tool for this?

Batch file to copy directory with files from one location to the current directory the file is in

I am trying to create a batch file that I can place in a folder and have it copy files the directory structure and the files from a networked location. I know this can be achieved using xcopy the issue I am having is it is not placing it in the folder where the batch file was copied. How can this be achieved so that I don't have to rewrite the file for every new folder I want this copied to? Here is what I have so far.
#echo off
Title xcopybatch.bat
REM Copy Directory Structure using xcopy Windows native command
REM -------------------------- Enter Source Directory ---------------------------
set "dir1=K:\GIS\Dallas Area 500-100 Year BFE Maps\_Misc Flood Info\"
REM -------------------------- Enter Dest Directory -----------------------------
set "dir2=K:."
REM ----------------------------------------------------------------------------
xcopy "%dir1%" "%dir2%" /e
pause
REM --------------------------------- Exit --------------------------------------
:end
EXIT /B 0
Based on your comments, you want to dump this script into a folder then run it, it will copy files from your source location, then self destruct (delete itself)?
By the way, if you're clarifying the parameters of a question, you should edit the question to reflect that.
with Xcopy, ommiting the target directory will assume you want the current working directory, but if you want to be sure, just implicitly specify %~dp0 as the target
xcopy "%dir1%" "%~dp0" /e
To have it delete itself, add this line at the very end
DEL "%~f0"
Just as %~dp0 give you the batch file's directory, %~f0 is the file name of the batch file. Because the CLI parses the entire file, it will run from memory, then delete itself.

How to copy files inside a folder without other folders in batch?

I am having the following folder structure:
Folder1
-Folder2
-File1
-File2
-File3
I have tried the following in batch script to copy all inside the Folder1
echo d | xcopy "Folder1\*.*" "DestinationFolder"/f /s /y /r
But I need to copy only File1, File2, File3. Need to ignore Folder2. I don't have any idea about how to achieve this. Please help me resolve this.
If Folder2 is empty, /S will do,
but since you have /s in your command, so I guess it's not empty.
So use this:
echo \Folder2\>__tmp4Exclude__
echo d | xcopy "Folder1\*.*" "DestinationFolder" /f /s /y /r /EXCLUDE:__tmp4Exclude__
del __tmp4Exclude__
__tmp4Exclude__ is a temp file created to contain the list of files to exclude while copying.
From xcopy /?:
/EXCLUDE:file1[+file2][+file3]...
Specifies a list of files containing strings. Each string
should be in a separate line in the files. When any of the
strings match any part of the absolute path of the file to be
copied, that file will be excluded from being copied. For
example, specifying a string like \obj\ or .obj will exclude
all files underneath the directory obj or all files with the
.obj extension respectively.
There is no need to use xcopy to copy just files! xcpoy is usually used to copy directory trees.
So, use copy instead. You can do:
copy "Folder1\*.*" "Destination\"
Probably you may need to use /(-)Y option:
/Y Suppresses prompting to confirm you want to overwrite an existing destination file.
/-Y Causes prompting to confirm you want to overwrite an existing destination file.
From copy /? (copy help page)

Copy subfolders with exclusions

I have a messy situation on production server, there is a location on production which should only contain 3 folder however due to not following some process we have got more than 1000 folders and files and I am aiming to clean it via a batch file so that there is no chances of human error.
So I would like to copy all folders and files except 3 folders to a new location. can someone help in this as not able to put logic to exclude these 3 folders.
Create a file called ex.txt that includes 3 lines, each of which is the name of the folder that you would like to exclude from the copy, e.g.:
\folder1\
\folder2\
\folder3\
Now, go to the parent of the high-level directory (say, directory_to_copy), that you would like to copy, in which location exists the ex.txt file, and type
xcopy /e /i /exclude:ex.txt directory_to_copy destination_name
This will exclude the folders folder1, folder2, and folder3 from the copy.
Note: the backslashes \ are important to ensure that the other folders containing those strings (folder1, folder2, and folder3) are not excluded.
This writes tempmove.bat.txt in the same folder as the batch file that contains the move commands to move every folder except the three folders shown (testenv stageenv prodenv).
You can examine the text file before renaming it to .bat to actually use, if it shows the right commands.
Make sure "d:\wrong folders" folder already exists.
#echo off
cd /d "c:\production folder"
(
for /d %%a in (*) do (
if /i not "%%~nxa"=="testenv" if /i not "%%~nxa"=="stageenv" if /i not "%%~nxa"=="prodenv" echo move "%%~fa" "d:\wrong folders\%%~nxa"
)
)>"%~dp0\tempmove.bat.txt"
pause

Compare filenames with foldernames and move matching filenames elsewhere

I need to compare filenames (minus extension) in folder A with foldernames in folder B.
If the filename minus extension has a matching foldername in folderB then move it to FolderC.
Example:
I have 3 directories: A, B and C
In folder A there are *.txt files.
In folder B are several folders
if a foldername (in folderB) has the same rootname as a filename in folderA then move the folderA file to folder C.
This should do it from the cmd prompt. Double all % to %% if it is inside a batch file.
for /d %a in ("folderb\*") do if exist "foldera\%~nxa.txt" move "foldera\%~nxa.txt" "folderc"
Answer 2
Your task (in this answers comments) can be turned around, to delete the .apk files that do not have a matching folder in _Dicts folder.
If that is what you need to do then test this:
#echo off
for %%a in ("_INPUT_APK\*.apk") do if not exist "_Dicts\%%~na\" del "%%a"

Resources