Search file, move folder/files - batch-file

I have my games on an HDD. I want to move the game I want to play to a SSD before playing.
I wonder if it’s possible to create a batch script which includes these steps:
Search for x file in x folder
If found open it
If not found move x folder to x folder
After moving open x file
Okay here is the code I put together. It seems to work. Not sure if this is the easiest/most efficient way.
Go easy on me, this is my first time. :)
#echo off
if exist "C:\Games\A Plague Tale Requiem\APlagueTaleRequiem_x64.exe" (
call "C:\Games\A Plague Tale Requiem\APlagueTaleRequiem_x64.exe"
) else (
xcopy /e "D:\Games\A Plague Tale Requiem\" "C:\Games\A Plague Tale Requiem\"
call "C:\Games\A Plague Tale Requiem\APlagueTaleRequiem_x64.exe"
)

Related

Windows 10 Updated - Now PDFTK batch file broken

I have a batch file that runs a simple "burst" (a.k.a. split) multi-page PDF into single pages scripts, then moves them to a mapped network drive (Z:\). This batch file is triggered by the user and has been working without a hitch for at least two years. Last week, there was a flurry of Windows 10 (x64) updates and it stopped working. Now, instead of looping over every PDF in the source dir, the batch file will generate single pages for the first PDF in the iteration, then stops (although not showing any errors in output). I have tried a multitude of fixes, including using a full timestamp in the target file names to prevent overwriting, though I don't think that's the problem. It seems too coincidental that it just stopped working shortly after the updates (per my User). I'm not very experienced with batch files, and just drew this up based on a PDFTK example I saw. Here it is (not sure why line 2 is being split in the markup, but in the batch file lines 2 & 3 here represent a single line; PS the last line is also being weird, should read: del "C:\Users\My User\Desktop\PHYS_SRCDIR'BACKSLASH''STAR''DOT''STAR'" /F /Q):
cd C:\Users\My User\Desktop\PHYS_SRCDIR
for /r %%i in (*.pdf) do (pdftk "%%i" burst output "Z:\PHYSICALS_IN\%date:~10,4%%date:~4,2%%date:~7,2%_%%~ni_%%03d.pdf")
del Z:\PHYSICALS_IN\doc_data.txt /F /Q
del "C:\Users\My User\Desktop\PHYS_SRCDIR\*.*" /F /Q
I haven't seen any new responses lately, and have to divert my attention elsewhere, so my solution was simply to move the batch file to the server w/ "Z:" on it (allowing a limited login to the User), reverse it so it copies from User's PC mapped drive to the Server, and move on to the bigger fish I have to fry. I know, it's not really a solution but that is my circumstance. I'll check in from time to time to see if there are any other suggestions. Since the batch file works perfectly from a WS2012R2 box my only thoughts are that something in Windows 10 "broke" either PDFTK Server or batch files/command-line in general. Since it works singly and only breaks when trying to loop over several multi-page PDFs, I'm leaning toward batch files/CLI. Thanks for the suggestions, keep 'em coming!

Deciphering "Make EXE from BAT" Script written by Jason Faulker

I came across a way to convert my .bat with dependencies on tool to an .exe file. However when I try using the script and run the .exe created, I always getting an error. Seems I modified the script incorrectly.
Anyone can help, please?
Here's the code with my modifications:
#ECHO OFF
ECHO Make EXE From BAT
ECHO Written by: Jason Faulkner
ECHO SysadminGeek.com
ECHO.
ECHO.
REM Usage:
MakeExeFromBat BatFileToConvert -bat MyProgram.bat
REM
REM Required Parameters:
BatFileToConvert -save MyProgram
REM Source batch file to use to produce the output Exe file.
REM
REM Optional Parameters:
IncludeFile -include Tool.exe
REM Additional files to include in the Exe file.
REM You can include external tools used by the batch file so they are available on the executing machine.
SETLOCAL
REM Configuration (no quotes needed):
SET PathTo7Zip=C:\Desktop\
REM ---- Do not modify anything below this line ----
SET OutputFile="%~n1.exe"
SET SourceFiles="%TEMP%\MakeEXE_files.txt"
SET Config="%TEMP%\MakeEXE_config.txt"
SET Source7ZFile="%Temp%\MakeEXE.7z"
REM Remove existing files
IF EXIST %OutputFile% DEL %OutputFile%
REM Build source archive
ECHO "%~dpnx1" > %SourceFiles%
:AddInclude
IF {%2}=={} GOTO EndInclude
ECHO "%~dpnx2" >> %SourceFiles%
SHIFT /2
GOTO AddInclude
:EndInclude
"%PathTo7Zip%\7za.exe" a %Source7ZFile% #%SourceFiles%
REM Build config file
ECHO ;!#Install#!UTF-8! > %Config%
ECHO RunProgram="%~nx1" >> %Config%
ECHO ;!#InstallEnd#! >> %Config%
REM Build EXE
COPY /B "%PathTo7Zip%\7zsd.sfx" + %Config% + %Source7ZFile% %OutputFile%
REM Clean up
IF EXIST %SourceFiles% DEL %SourceFiles%
IF EXIST %Config% DEL %Config%
IF EXIST %Source7ZFile% DEL %Source7ZFile%
ENDLOCAL
This doesn't really convert a bat file to an exe. It just creates a selfextracting archive (exe) which contains the bat file. On execution it extracts the file to a temporary folder and runs it from there. You can even extract the bat from the exe just by using 7zip/rar/winzip or any other archiver.
If you want to convert a bat to an exe for real you should use one of the tools from the web (like this one: http://www.f2ko.de/index.php?lang=en) or concider using a simple script language like AutoIt.
If you pick the second, you can simply execute your bat code with Run("put your bat code in here") and you can compile your script to a "real" exe file.
For an alternative approach, you can basically do the same thing as described in the accepted answer (making a 7z-SFX) with WinRAR. That way, you can also do it with a GUI, and I will try to add some more useful information.
Actually, you can also use the latter approach to generate portable applications and it also works with "converting" every runnable (or openable) file into an .exe.
If you need that "portability hack", you should unpack your .exe or .msi installer with Universal Extractor. Details can be found in this Article, Step 1 to 4. Newer Versions of 7zip or WinRAR also come with comparable features.
Now you add all needed files to the archive. In the easiest case, this is just your .bat script or whatever file you want to "convert" into an .exe applivation. (Step 5 here)
Steps 6 and 7 are just some Settings for the SFX-Archive, 8 is the interesting one, as you select what you actually want to run there. Input the name of your (.bat-)file.
Step 9 lets you select where to unpack to - you do this setting manually and programmatically in the MakeExeFromBat.bat-script.
After this process you created a Portable App in SFX archiever form, enjoy
The word "converting" was put into quotation marks, because running that .exe actually works like this:
The contents of the (SFX-)EXE file are extracted from the "archive part" to a directory as the specified temp directory.
( The config file generated by the script is read. )
The file, that was previously contained in the EXE file and then extracted, is now executed in a new window.
a) This file could besides a .bat be anything - as e.g. an image, a MP3 or a video
b) or also a Python Script (of course your OS needs to know how to deal with that file.
Once finished, the temp files are removed.
You can also derive some limitations from that. If you have a .bat that needs the content of the working directory, you will have a problem. (Say, a batch that renames all files in the current dir from 1 to n.) In some cases that can be dealt with by adding all needed files to the archive too. On Windows Vista and all newer OSes, you might encounter a message box after the script is run. After selecting ‘This program installed correctly’, the message box will not be displayed in the future for this file. Because the EXE file launches in a new window, the typical way of logging output (using the > char) will not work as expected. In order to log the output, you would need to handle this natively in your source script.
All references were already linked, but once again: Big credit goes to Jason Faulkner for providing the Article and 7zip-Approach, binbert for the WinRAR-SFX Solution (which is as hinted much more versatile -> portability) and some credit to creative8 for finding the two and the article comparing them.
Actually, I was develping another solution using AutoHotkey. In my case, I just want to be able to add my .bat to the windows start menu - but the options are not limited to that.
The script itself is just a oneliner and .AHK is easily converted to .exe (I used v1.1.33.09):
run % SubStr(A_ScriptName, 1, -4) ;// run also has the option to run your file minimized or hidden, see the source 2 below
Source 2
What it does is taking its own name, removing the .ahk or .exe respectively (the last 4 characters, hence -4) and running excactly that. Usage could not be easier: you have a runme.bat, so you rename the program I provide to runme.bat.exe. Say you want the .exe to open an image.png - guess what, rename it to image.png.exe. You get the gist - that's it. It dynamically checks its name to find what to run. In my opinion, this is not much less mighty than "unpacking the .bat and then run it", but (again imho) it is much more elegant.
Use it as you wish, I should probably start a public github page or so.

Batch move files based on part of file name - Windows 8

I've got a number of files I'm trying to sort through for my PhD research. I'm not familiar with using Batch files but I do have some coding experience.
I'm trying to move the files based on part of their filename into folders generated automatically for them in the directory they're currently in.
The files were generated as part of a series of pXRF analyses on brass objects held at a museum.
Some examples of the file structure:
01-64.1007-1-LID.pdz
01-64.1007-2-BASE.pdz
02-67.1765-1.pdz
02-67.1765-2.pdz
02-67.1765-3.pdz
etc.
Basically the file structure works like this:
The first number is my unique identifier for the object, then a hyphen, then the museums unique identifier, then a hyphen, then a number to show which analysis of the object. Finally, there may or may not be a small descriptor (i.e. BASE or LID) which helps me identify where I performed the scan.
So to break it down 01-64.1007-1-LID means Object 1 - Museum Accession Number 64.1007 - First Analysis - Performed on the lid of the object - .pdz is the file extension.
02-67.1765-2 means Object 2 - Museum Accession Number 67.1765 - Second Analysis etc.
What I want to do is move:
01-64.1007-1-LID.pdz
01-64.1007-2-BASE.pdz
into a folder called:
01-64.1007
and
02-67.1765-1.pdz
02-67.1765-2.pdz
02-67.1765-3.pdz
into a folder called:
02-67.1765
etc.
I've seen a few scripts which may help with this but I'm unsure how to tweak them for my purposes. If anyone can help I'd really appreciate it!
Thanks,
Mike
Test this batch file in a folder of sample files: call it movepdz.bat so the name doesn't clash with another executable.
#echo off
for %%a in (*.pdz) do (
echo processing "%%a"
for /f "tokens=1,2 delims=-" %%b in ("%%~nxa") do (
md "%%b-%%c" 2>nul
move "%%a" "%%b-%%c" >nul
)
)
pause
If you really mean a bash script, you can use something like this:
for file in *.pdz; do
dir=${echo "$file"|cut -d- -f -2}
mkdir -p "$dir"
mv "$file" "$dir"
done
The cut command will cut out the part before the second hyphen to determine the directory name. Then we create the directory in case it is not already present. Finally simply move the file in that directory.
Update: Thanks for clarifying your environment. (I was sure the question mentioned bash before...) - If you want to use this solution you could install git which comes with a nice git-bash.

Help with Batch Files?

What are batch files useful for? They just seem to be used to make viruses and other things...but it seems like shell scripting to me.
Whats the uses for batch files?
From Batch file article at Wikipedia:
Batch files are useful for running a
sequence of executables automatically
and are often used by system
administrators to automate tedious
processes. Unix-like operating
systems (such as Linux) have a similar
type of file called a shell script.
A simple example:
for /f "tokens=* delims=" %%i in ('dir /s /b /a:d *svn') do ( rd /s /q "%%i")
If you save the above line in a file called ClearSVNFolders.bat and after that execute a double click you'll delete every folder named svn that resides inside a root path...
You automated the whole process. You could easily spend hours doing the above task if you had a deep root directory, that is, one containing thousands of folders. :)
Batch files are the Windows equivalent of a Unix shell script. So you can use them to automate things.
You could use them for shell scripting. :-P
Of course, they kind of suck at that, compared to bash (or perl/python/tcl). But if you're on Windows, it's a one-horse race unless you want to install cygwin or msys and battle with Unix/Windows incompatibilities.
Batch Files are extremely useful. They are super easy to learn as well. you can make them do things on startup like say that a program wants to open itself and wont close even from taskman.exe you can force it to shutoff without warning.
or you could make games and ineractive things like i like to do.
i have a Messenger that i made with fully customizable colors and accounts with account management and servers.
But you probably dont trust me enough for you to download it.
But yea they are pretty useful.
Batch file is "a computer file containing a list of instructions to be carried out in turn."
We have been studying since childhood that computer is a dummy machine and this is a method of instructing a dummy machine.
For example :-
If you want to instruct the system to create a folder with random name then type ,
#echo off
md %random%
Creating Batch files enables you to execute several line of CMD commands in a single file.
For example :-
#echo off
md %random%
tasklist
Pause
The entire purpose of a Batch script is to execute several DOS commands in sequence:
echo Hello!
set var=7
echo I just made var=%var%!
pause
It was invented in MS-DOS for user simplicity to execute things they did all the time, the most notable thing being "AUTOEXEC.BAT" which started once the command interpreter started, people would add things like:
echo Welcome to my computer!
or
cd C:\Games\
To make it quicker to access their games or whatever they needed.

How to move all mp3 files into a single directory?

I have a bunch of MP3 files split up into artist\album, and I want to move these all into a single directory, and get rid of the directory itself, using a windows batch file (hence the tags)
You can start from:
for /R %%x in (*.mp3) do move "%%x" "c:\dir"
Use the Windows search function to search for *.MP3, wait for it to finish. Select all results and use cut. Paste into the target directory.
Then the subdirectories should be empty. You can select them all at once and delete them.
For a bit of an overkill of an effort, install any Unix utilities (e.g. CYGWIN, many oithers) and do "mv //* final_dir" :)
Of course, you will be left with a highly useful and uber cool set of unix utilities for Windows.
Another overkill is t install ActivePErl and do it in Perl:
map { move($_, $final_dir) || die "Can not move $_: $!" } glob("basedir/*/*/*");
fs-dependent, filenumberlimit experimental result was you can have thousands of files same level here, didn't try > 10000, > 1000 ok
EDIT I see you want to do wit with "win batch" (in one of your comments added later)... I leave my answer up as an alternative...
I've used JP soft's 4NT (a command.com replacement) to do this.
cd <root of mp3 tree>
global /i move *.mpr \newdir
just beware that newdir aboslutely must not be a child of <root of mp3 tree>
global executes a command (the move command) in every subdirecotry of the starting directory. /i tells it to ignore returncodes (a directory might contain zero mp3 files).
4NT is nolonger sold but "Take Command" should work also.
artist>move *.mp3 destinationDirectory will work I believe.
This should be moved to superuser, first off. Second, I use MusicBrainz for my mp3 library.
Since the question has gotten more complex, let me elaborate on MusicBrainz.
You point it at a music folder, deep as you want, and it grabs all songs found in that directory. It then offers to retag them based on it's user-generated DB. It uses some crazy method of audio finger printing to guess any songs that either lack meta-data or need the right meta-data (say goodbye to Aretha Franklin doing "Son of Preacher Man" and the famous Rolling Stones cover of "Brown Eyed Girl").
After finishing up with any meta data correction, you hit save, and it will:
a) replace/add the meta data tags
b) move your mp3 files into directories based on any pattern you specify
c) if you set this, it will delete any folders that it leaves empty upon file relocation
So, you could simply tell it NOT to retag and NOT to use any meta data for folder destination, and it will all that you want (and more if you want it to).
I have mine set to grab stuff from my "Giant Music Mess" folder and then put them into folders based on artist, album, disc, and finally give the mp3 file a "track# - title" rename. Something like Music Library/%Artist%/%Album%/%Vol%/%#% - %title

Resources