Moving files at intervals - file

I am a real novice compared to people on here but I am able to navigate the command line and perform basic actions like move, copy, delete files and folders using the relevant commands. I am aware of the ability to use batch files and I was wondering if anyone coyld help me solve a problem at work. I dont work in IT by the way but I just know there has got to be an easy solution to this from the experience I do have.
Without going into the specifics too much my question comes down to this. I need to move one file at a time(basically a split pdf) from one folder to another at the rate of one per hour. So there may be up to 20 files all in one folder then one gets moved one hour, another the next and so on.
Any help would be greatly appreciated. :)

What you're looking for is probably a cron. Cron's function is to run a certain command on a certain interval. To move a file once per hour you would use a crontab which is a file that specifies what to run and how often to run it. your cronfile might look somthing like this 0 * * * * <your move command>. The to execute the cron you'd do somthing like contab /path/to/your/crontab/file

Related

Batch copy files

I'm writing a batch file to perform recurring operations with choice menu options etc...
but I got stuck on two points.
I tried to read up, but I don't know exactly how to ask questions using search engines.
I try to explain myself.
Problem 1
I need to copy files to a known location, I'd like the batch to stop at some point
and asked me the default source is D:, do you want to copy from here or elsewhere? if so, insert new path
Problem 2
I'm working on Windows .wim images, the command below, as you know, is used to export a single index of these images.
When this command is executed I would like it to stop and ask me if that 4 can go well, or if not
gave me the opportunity to enter another value.
dism /Export-Image /SourceImageFile:d:\Sources\install.wim /SourceIndex:4 /DestinationImageFile:C:\test\install.wim /Compress:Max /CheckIntegrity
At the moment I have not solved the problem.

Batch adjust times on files

I just filmed a wedding, using two different cameras, and realized after the wedding that I corrected only one camera for daylight savings time. My editing software automatically (albeit roughly) arranges the video clips on the timeline based on the time stamps in the file's properties. It would save an incredible amount of time if I could add one hour (plus/minus a couple minutes or seconds, the time synchronization was apparently not perfect) to all of the files in a given folder.
I've found some freeware online that allows me to change all of the files simultaneously, so it resets every file to have the same creation time, rather than just add some preset time to each file. I am not very apt to use or run code, so please keep that in mind when providing ideas! Any and all are greatly appreciated!
Thanks in advance!
You can use a powershell one-liner to do what you need I think.
Open a cmd console and cd into the directory containing the clips you want to modify. Then to add an hour to every file's timestamp you'd just
powershell "gci | %{ $_.LastWriteTime = $_.LastWriteTime.addHours(1) }"
You can do the same thing with .addMinutes as well if you wish. If you want to manipulate the last modified time of an individual file, instead of using Get-ChildItem | foreach (or gci | % as I have it shortened), do
powershell "$f = gi filename.ext; $f.LastWriteTime = $f.LastWriteTime.addHours(1)"
If you mess up and need to subtract hours, do .addHours(-1).
If you have questions, please bear in mind that this is a programming website. If you are uncomfortable navigating the windows file system through a cmd window or need other basic assistance, then SuperUser would probably be a more appropriate place to ask.

Delete different named folders using batch

I've been doing a little research, and I haven't find anything remotely similar to what I'm looking at, so here's my case:
I have a folder, lets name it MasterFolder
So, lets say that my MasterFolder, has inside of it, four more folders, I know I can use
cd C:\MasterFolder\
to leave my CMD console just on top of those so, for didactic meanings, lets call them:
- Folder_A
- Folder_B
- Folder_C
- Folder_D
So, inside each of those folders, there are even more folders, that are named
"EncodingMaster_Originals"
the name is the same for all, so, that way, we could say that the folders are, for example:
- Folder_A\EncodingMaster_Originals (and OTHER files that I won't delete)
- Folder_B\InsideFolder_B\EncodingMaster_Originals (In another folder inside Folder_B
Those two are the most simple examples.
So, what I want to do?
Well, is simple, I just want to delete the folder named
"EncodingMaster_Originals"
from each of the folders, without having to be going into each one (There's around 200 folders with those little ones inside, I've made some massive re-encoding of files, and those folders appeared from nowhere).
I think that I've to use this line of code:
rd
(the Remove directory command), but my problem is that I don't know what I have to tell to the RD command, I don't want to look more dumb so I won't type what I've tried, because it was obviously wrong.
If you could help me, I would appreciate it.
So, after a brief search with a different bunch of words. I found THIS post:
How to remove all folders of name x within a directory using cmd/batch file
And the answer is this comment:
https://stackoverflow.com/a/10399706/4625105
Thanks if you were about to answer, I think the case is closed.

How to refactor a Windows batch script littered with GOTOs?

I have to maintain a batch script of about 3500 lines littered with GOTO. Seems that the original "developer" hasn't heard of this famous paper and modular programming.
What the script does?
The script deals with the (silent) installation/uninstallation/reinstallation of several programs using different options. It could be split in several files that deal with each program in part. The problem is that if you're trying to take a part in another file that part will still GOTO another section that needs to be in the original script.
Refactoring?
Normally you wouldn't do a refactoring without having automated tests (so you can be sure you didn't break anything), but I don't know how to do it. There's no testing framework for that.
Partial Solution
I have come up with a partial "solution" that is some kind of adaptation of characterization tests (from Working Effectively with Legacy Code by Michael Feathers) and approval tests:
- create another script: test.py that replaces all commands (like copy or msiexec) with echo,
- redirect the output to a text file (good.txt),
- change the original batch script,
- run the test.py script again and save the output to another text file (current.txt),
- diff good.txt and current.txt -> if there are no differences then I didn't break anything, but if they are different I need to check if I broke something.
Problem with partial solution
How can I capture and replace all the commands? I could make a list of commands to replace, but there are also a lot of string concatenations to get the name and path of the program to be installed.
CMD level capture/hook?
Is there any way I can hook into the command line interpreter (CMD.exe) so I can replace on the fly all the calls to installers with echo?
Other suggestions?
Do I approach the problem in the wrong way? Can I do it better somehow? Do you have some advice I could use?
You could replace all COPY, DEL or CALL with %COPY%, %DEL% ,...
So you can use the same file for production and also for the tests.
#echo off
if not defined UNITTEST (
set "COPY=COPY"
set "DEL=DEL"
set "CALL=CALL"
)
%COPY% src dest
%DEL% somefile.txt
%CALL% installer.exe
And from your unittest.bat, you could start it via
#echo off
set "COPY=>>trace.log ECHO COPY"
set "DEL=>>trace.log ECHO DEL"
set "CALL=>>trace.log CALL ECHO "
del trace.log
set "unittest=Active"
call production.bat
fc good.txt trace.log
I'm not an expert in Batch, but I have done my fair share of it. With that said, I can offer a few tips.
Forget trying to do it all at once. Batch is very hard to debug. Echoing out to a log file helps a lot, but it will not capture everything you need if something goes wrong.
Work on breaking out the exe and msiexec calls into self-contained scripts. It is much easier to test the small script for the functionality you desire. Once you have that working, it is simple to call that script from the "Master" script.
Establish a good protocol for passing args to, and return codes from the smaller scripts. If there are common settings needed to be used for all the scripts consider using a central settings file.
GOTOs are not the devil, unless they pass control all over the place. Normally there are two good reasons that I know of to use GOTO’s.
Skip past a block of code that does not need to run.
To SET values into variables. Note there is a bug that can prevent variables from having their value set from within an 'IF' statement block. That little bug caused a big headache for me at one time.
Calls to a label might be better option at times.
Depending on how far back the legacy support is required, consider using Powershell when possible. The power and debugging capabilities of Powershell far out way the benefits of simple scripting of Batch. Which at 3500 lines simplicity has already been lost. You are already looking at Python, so maybe that could be used instead.
If you need a break point, use Pause. ECHO all the settings you need to examine right before the pause. This is as close to a break point I have found for batch.
Echo the command you intend to run to a log file and actually run it.
Write small verification scripts to be used independently or with the “Master” script to confirm you are getting the results you are expecting.
Use the right tool for the job. I like to use EditPadPro, RegexBuddy, and BeyondCompare for batch editing and comparing differences. There free tools that can be used too NotePad++ and Windiff. Making many edits in a file of that size is best handled by a good editor. IE inserting an echo at the beginning of a line that calls a cmd.exe.
Remember it is scripting not programming. While there is a lot of overlap of the two, the same exact approach to a problem may not be viable between the two.
Always make a backup copy of the scripts as a whole before mucking around. A fallback position is greatly appreciated when there is one small bug that you can’t find.
If it ain't broke... well you wouldn't be working on it if everything was working just fine.
Always test changes. And when you are done test it again. After that have someone else test it.
Just my .02. I’m sure someone else can chime in with more advanced advice. My knowledge on Batch has been acquired from the school of hard knocks, supplemented by ss64.com

(Batch) How ombine multiples different file types into one package?

#echo off
Hello World!
For the past couple days I've been messing with Batch. I made a text based game where you make choices that affect the game and user.
I need to take multiple different file types. Compile or combine them into a single executable. I don't want to send this out as a .rar. If the user would view them it could spoil the story.
For example:
Game.bat, is looking at:
intro.txt
character1.txt
character2.txt
character3.txt
character4.txt
sound1.mp3
sound2.mp3
Perhaps I went at this the with the wrong method?
Many thanks!
false_positive
pause > nu; :P
So I'm not entirely sure if this will answer your question, but I was looking for something similar to group together a bunch of batch files into a single executable that starts with a menu batch. But this site here walks through how to use the program iExpress which is built into windows from the days of DOS I guess. anyways It was enough to do what I needed it to. Best of luck anyways!

Resources