I faced an interesting task recently. For each file sent over via the ftp channel I was supposed to generate an empty *.OK file and send it over immediately after the main file transfer was completed.
This allows the receiving party to process the file on their side with minimum delays.
Let’s say we are sending 5GB of csv data in the CUSTOMER_2016.csv file. As soon as the transfer is done, we need to send an empty CUSTOMER_2016.csv.OK file.
How to create an empty file, using just built-in Windows OS techniques, without installing any 3rd party thingies?
There’s a whole bunch of methods. I am going to list them here. Most of them actually work!
Let’s start with the non-working ones:
1. Echo an empty string and redirect the output to the OK file:
Result: a 13-bytes file with “ECHO is on.” (without quotes) text in it, ended with a 2-byte EOL combo.
2. Echo an empty string in quotes and redirect the output to the OK file:
Result: a 4-bytes file with “” in it (just the quotes) plus the EOL combo.
3. Echo anything and redirect error output to the OK file:
echo blahblahblah 2>CUSTOMER_2016.csv.OK
Result: an empty CUSTOMER_2016.csv.OK file (yay!) and some “blahblahblah” garbage in the console window.
(partial as we would like to see no garbage in the main window – it can potentially clash with some logging engine once the task is automated)
4. Echo a blank string and redirect error output to the OK file:
Result: similar to the last one but instead of “blahblahblah” we see “Echo is ON.” message.
5. Use an empty comment and redirect error output to the OK file:
Result: no garbage on the screen and an empty CUSTOMER_2016.csv.OK file created.
6. Stop the script (using BREAK command) and redirect its output to the OK file:
Result: same as above but (probably) a bit faster as it does not require using error output nor executing the dummy REM command.
7. Use the COPY command with special NUL file as a source
copy /y nul CUSTOMER_2016.csv.OK
Result: same as above but without using redirection at all. Remember to use the /y switch in case the OK file already exists.
8. Crack an egg with a sledgehammer i.e. use the fsutil command:
fsutil file createnew CUSTOMER_2016.csv.OK 0
Result: same as above (if the CUSTOMER_2016.csv.OK wasn’t there yet) or an error message if the file was already there. Advantage of this method: it will run even if the target location of the OK file is on a mapped drive (some of the previous methods may not work on mapped drives)
As one can clearly see, there’s many ways to skin a cat and some of them result in the cat being actually skinned. Some other just leave a mess behind and a dead cat.
What’s the Linux ways of achieving this?
A little bird told me that bash is coming to Windows 10 sometime soon. We’ll see.