Windows Batch File - Move files with same file name but different extension - batch-file

I have written an small batch file that moves all content from one folder to another.
This works fine. However, the source folder contains two types of files. One of the types is .doc and the other is .xml. Both files have the same name. But sometimes, one of the files (either .doc or .xml) is missing.
#echo off
move /y "\\networklocation\folder\folder\*.*" "M:\localfolder"
The question is how to make my script move only couples of .doc and .xml files that have the same name. For example, the source contains 1.doc, 2.doc and 1.xml. The script should only move 1.doc and 1.xml. 2.doc should stay in the source folder.
I've looked for this particular problem but have not found really anything.

Try this:
#echo off &setlocal
for %%i in ("\\networklocation\folder\folder\*.doc") do (
if exist "%%~dpni.xml" (
move /y "%%~i" "M:\localfolder"
move /y "%%~dpni.xml" "M:\localfolder"
)
)

Related

7zip bat / command compress multiple folders to multiple corresponding archives without folder in archive

I'm often compressing folders of images or other project related documents into separate zip files the current command in a bat file I'm using is.
for /d %%X in (*) do "D:\Program Files\7-Zip\7z.exe" a "%%X.7z" "%%X\"
It automatically places the full contents of all folders separate archive files.
My question is if you double click on any of the archives it first navigates to a a folder of the same name as the archive. Is there a way to make it so it doesn't put have to folder, it just has the contents?
Right now I get
D:\User\1501.7z\1501\ contents
I just want
D:\User\1501.7z\ contents
Second if this is possible can it be set up so that if one of the folders has multiple folders in it all the contents of each folder is placed into just the on directory instead of having a multiple folders in the archive.
Thanks,
Tony
This will set the working directory to the folder that you want to zip up and save the zip file one level up from that directory.
#echo off
for /d %%X in (*) do (
PUSHD "%%X"
"D:\Program Files\7-Zip\7z.exe" a "..\%%X.7z" "*"
POPD
)

How do I use a Batch Script to Move Specific Files with Specific File Contents

I am looking to find a file with the contents 'Radiac' in one particular folder and then move all those files to a another folder.
For example: I have might have 3 identical file-names with different extensions like below. Let's say, the content Radiac is only in the jones.pdf file. What I am looking to do is find the file-name with this specific content then xcopy this filename.. and its associated extensions to another folder.
jones.pdf
jones.xml
jones.txt
jones.doc
I was able to accomplish that with the following script:
for /f "delims=" %%a in ('findstr /m /c:"Radiac" *.*') do ^
xcopy "%%a" "C:\"path\to\my\folder"
However, I was unable to move files with the .pdf extension. Is there script that will move any file regardless of their extension? I am new to batch scripting so excuse me if I miss anything.
Thanks

.bat coding or some app maybe?

how can I organize my directories automatically?, I have a "downloads" folder, which currently contains lots of different info, for example, work related info, tv-shows, movies, etc, software, etc.
How can I automatically, maybe using some .bat execution, not sure, check for example the name of the files, or the type, and put them in the right subfolders?.
Thanks!.
You can use the move command to move files. You can also use wildcards in it.
So you could write a batch script that looks something like this:
cd C:\Users\You\Downloads
rem Excel sheets are work.
move *.xls Work
rem Reports are work.
move Report*.pdf Work\Reports
rem Pron and other viewing material ;)
move *.mp4 Private
You could run this script automatically by making it a scheduled job. Note that this script changes to the right directory first and then moves items to a relative subdirectory. This means that the directories Work, Work\Reports and Private must exist in the Downloads directory.
Of course you can expand the script to make it check and create directories as well, or you can specify different paths if you want to move the files out of the Downloads directory.
Basically, try to do it on the command line and then try to generalize those steps into your script.
This batch file will create a set of folders like .mkv .jpg .mp3 using the filetypes inside the folder, and move the files into the appropriate folders.
Launch it from your desktop and change the "c:\media" to the right folder name.
#echo off
cd /d "c:\media" && for %%a in (*) do md "%%~xa" 2>nul & move "%%a" "%%~xa" >nul

Batch File move files base on part of their name

Is it possible for a bat file to search through a folder and look at the file names and only move files with that name or part of that name in it? Then move them into a specified location.
For example:
Parent Folder
Arrow0273.text
Arrow0314.text
Spear083112.text
Spear0832.text
Sheild087.txt
Sheild87.txt
Move only the files with “Arrow” in their name into folder location “A”.
ect...
Thanks Guys!
Edit:
Found this but not sure if it is what I'm looking for, and to be honest, not sure how that code works. Move files to directories based on some part of file name?
Maybe something like this? I found this on here a while ago, and I use it all the time. It will move files into a folder based on the file name and type. If the folder doesn't exist, it will create the folder in the current location of the batch file. If the folder already exists, it will simply move it to that folder.
#echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.text') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~0,1!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!" >nul
endlocal
)
The ".pdf" in line 2 can be changed to specify the file type. You may use ".*" to move all file types, though this will also move the .bat file and folders.
The "~0,1!" in line 5 determines which characters are looked at to determine the folder names. The first number determines which character it begins looking at (0 is at the beginning, 1 is 1 character from the beginning, etc). The second number determines how many characters it looks at. If it was changed to 2, it will look at the first 2 characters in the file.
Currently it is set to only look at the first character and move only .text files. For the files in your example, it would move all of the "Arrow" files for a folder named "A", and all of the "Spear" files to a folder named "S". The "Shield" files would stay where they are, as their extension is .txt, not .text. If you changed ".text" to ".t*" it will move both the .txt and .text files into the "A" and "S" folders.
copy supports wild cards so all you need to do is:
copy Arrow* A

Folder Names Based on Filenames

This isnt really a coding question. I need to put all my files into individual directories so each file has its own directory with the name based on the filename.
So before i go make an application to do this, does anyone know any software that can do this? like Automator or something.
No need to build an application. This simple one liner run from the Windows command line will move each file in a directory into a sub-directory based on the root name of the file.
for %f in (*) do #(md "%~nf"&move "%f" "%~nf")>nul 2>&1
Two files with the same base name but different extensions will be moved into the same directory. For example, "test.txt" and "text.doc" will both be moved into a directory named "test"
Any file without an extension will not be moved.
If you want to run this from a batch file, then
#echo off
for %%f in (*) do (
md "%%~nf"
move "%%f" "%%~nf"
) >nul 2>&1
You requirements were not very clear. If your requirements differ, then the script can probably be modified fairly easily to suit your needs.

Resources