0

Not a coder but know the logic like understanding a language but not able to speak it. Environment is Windows server 2016.

Trying to write a .bat file to replace empty spaces with zeros '0' in a file name in a directory. Found a script online and modified. But when I run it I get an error below. Don't understand why. Any help would be appreciated. Thanks.

ERROR- "75553.txt was unexpected at this time."

    @echo off
    myfile="\\Dev\Out\File\Outfile-name-20200902 75553.txt"
    for /f %myfile% do (mv "$f" "${f// /0}") done
    echo %myfile%
    pause
jeb
  • 335
  • 3
  • 10
user590590
  • 11
  • 1
  • Provide some info on platform and kind of shell or base utils you have available. This most probably can be done with a bash one-liner in a generic Linux distro. – Ali Sattari Sep 07 '20 at 12:31
  • @AliSattari - Its a Windows Server 2016. The one liner command if possible I need to enter into the Informatica POST processing command line for a Data Synchronization Task. – user590590 Sep 07 '20 at 19:49
  • This looks like a question for [so], not here. We have precisely zero people who know anything about Informatica, [so] has quite a lot. – Michael Hampton Sep 08 '20 at 13:35
  • The question is not about Informatica. He tries to rename files with a batch script, which is in scope for this site. – Daniel Sep 10 '20 at 05:18

2 Answers2

0

I would do it in multiple steps:

  1. Save the existing file names in the directory to a file.
  2. For each filename, temporarily store a copy of the filename with any spaces replaced by "0" characters.
  3. If the temporarily stored filename differs from the current filename in the list, rename/move current filename to new filename.
Mikael H
  • 4,868
  • 2
  • 8
  • 15
0

If you really have got bash (per your tags) the entire process can be handled very straightforwardly. Here's a script that would work with Cygwin

#!/bin/bash
#
shopt -s nullglob

for src in //dev/inbound/outfile*.txt
do
    file="${src##*/}"                    # Get filename without path
    dst="//dev/outbound/${file// /0}"    # Generate target path, with zeroes for spaces
    mv -f "$src" "$dst"                  # Move and rename the file
done
roaima
  • 1,567
  • 13
  • 26
  • thank you for the script it works but management wants a .bat file now. It was fine before but something happened, not sure what. Sorry did not mean to have you waste your time writing the code. Thanks again for your time and effort. – user590590 Sep 08 '20 at 16:10