Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom script for rename files #1

Open
stodevCoding opened this issue Jul 14, 2021 · 1 comment
Open

custom script for rename files #1

stodevCoding opened this issue Jul 14, 2021 · 1 comment

Comments

@stodevCoding
Copy link

stodevCoding commented Jul 14, 2021

Hello, I came across your repo that promising me. This is the only one of its kind. But I can not understand how to customize the script for my need. I am still at the beginning of my studies in computer programming but I thought I was comfortable enough in Swift.

I would like to rename about 5000 files that have this scheme there:
"Randomtext randomtext randomtext 848484984984.txt" To rename them in "848484984984.txt".

So I thought I could apply this treatment here:

    public func componentsSeparatedByStrings() -> String {
        let separators = ["%20"]
        let arrayName = separators.reduce([self]) { result, separator in
            return result.flatMap { $0.components(separatedBy: separator) }
        }.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } // ["RandomText, RandomText, RandomText, 848484984984"]
        let newName = arrayName.last
        return newName! //848484984984
    }

Unfortunately I can only have this result in my terminal.
'RandomText Randomtext Randomtext 848484984984.txt ' not Renamed

Would you have the kindness to help me? I thank you in advance

@jpmhouston
Copy link
Owner

jpmhouston commented Jul 17, 2021

Thanks for checking out my project. I made it for my own use and was curious if anyone else would find it useful.

I'd suggest experimenting with a function in a Xcode playground. The kind of function you need is like (as defined by the typealias RenameCommand.RenameFunc):

function myrename1(_ name: inout  String, _ extension: String) {
  ...
}

In your ParsableCommand type, you'd end up using it simply like:

func run() throws {
  try options.runRename(myrename1)
}

Or anything like that.

It might make more sense to you, or might be easier to test & debug, or have a function like:

function myrename2(_ name:  String) -> String {
  ...
}

which you'd end up using in your ParsableCommand type like this:

func run() throws {
  try options.runRename() { name, _ in
    name = myrename2(name)
  }
}

From your email you seem to be trying to write an extension on String. If you indeed did that then you'd use like:

func run() throws {
  try options.runRename() { name, _ in
    name = name.myrename3()
  }
}

Whichever way you do it, writing this function separately in a playground lets you quickly iterate and try with new input until you're satisfied it will work, eg. by having the playground include just your function (doesn't need to import RenameCommand or ArgumentParser), and then follow it your own calls to it like:

myrename("RandomText Randomtext Randomtext 848484984984", "txt")

About the code you included in your email, its seem far too complicated and admittedly I didn't try to follow it. What I think you need is merely something like this:

function lastSpaceSeparatedComponent(_ name: String) -> String {
  let components = name.components(separatedBy: " ")  // don't need %20, a literal space is ok
  return components.last
}

Use it in your ParsableCommand object like my "myrename2" example above, combined with the other bits in the full example from my readme. Let me know if you're having any trouble with that and I can pretty easily give you the full solution.

I'm wondering, are you going to use this with swift-sh? This lets you have a swift "script" file you don't have to worry about having a project for, compiling ahead of time, finding the compiled executable, etc. If you've been paying attention to the Swift Evolution forums, or news about it, then you might have learned that this kind functionality might soon be built into the general "swift" command without having to add on the "swift-sh" homebrew package. Sadly, I don't think it will be ready in a shipping version of Swift for a while, later this year at the earliest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants